-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
272 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
namespace Aya.DataBinding | ||
{ | ||
public class RuntimePropertyBinder<TTarget> : DataBinder<TTarget, object> | ||
{ | ||
public string Property; | ||
|
||
public override bool NeedUpdate => true; | ||
|
||
public override object Value | ||
{ | ||
get | ||
{ | ||
if (FiledInfo != null) | ||
{ | ||
var data = FiledInfo.GetValue(Target); | ||
return data; | ||
} | ||
|
||
if (PropertyInfo != null) | ||
{ | ||
var data = PropertyInfo.GetValue(Target, null); | ||
return data; | ||
} | ||
|
||
return default; | ||
} | ||
set | ||
{ | ||
var data = value; | ||
var dataType = data != null ? data.GetType() : typeof(object); | ||
if (FiledInfo != null) | ||
{ | ||
if (data != null && dataType != FiledInfo.FieldType) | ||
{ | ||
var convertData = Convert.ChangeType(data, FiledInfo.FieldType, CultureInfo.InvariantCulture); | ||
FiledInfo.SetValue(Target, convertData); | ||
} | ||
else | ||
{ | ||
FiledInfo.SetValue(Target, data); | ||
} | ||
} | ||
|
||
if (PropertyInfo != null) | ||
{ | ||
if (data != null && dataType != PropertyInfo.PropertyType) | ||
{ | ||
var convertData = Convert.ChangeType(data, PropertyInfo.PropertyType, | ||
CultureInfo.InvariantCulture); | ||
PropertyInfo.SetValue(Target, convertData, null); | ||
} | ||
else | ||
{ | ||
PropertyInfo.SetValue(Target, data, null); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public FieldInfo FiledInfo | ||
{ | ||
get | ||
{ | ||
if (_filedInfo == null || _filedInfo.Name != Property) | ||
{ | ||
_filedInfo = TargetType.GetField(Property, TypeCaches.DefaultBindingFlags); | ||
DataType = _filedInfo?.FieldType; | ||
} | ||
|
||
return _filedInfo; | ||
} | ||
|
||
internal set => _filedInfo = value; | ||
} | ||
|
||
private FieldInfo _filedInfo; | ||
|
||
public PropertyInfo PropertyInfo | ||
{ | ||
get | ||
{ | ||
if (_propertyInfo == null || _propertyInfo.Name != Property) | ||
{ | ||
_propertyInfo = TargetType.GetProperty(Property, TypeCaches.DefaultBindingFlags); | ||
DataType = _propertyInfo?.PropertyType; | ||
} | ||
|
||
return _propertyInfo; | ||
} | ||
|
||
internal set => _propertyInfo = value; | ||
} | ||
|
||
private PropertyInfo _propertyInfo; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.