Skip to content

Commit

Permalink
Fix 'DataContainer is NULL' error
Browse files Browse the repository at this point in the history
  • Loading branch information
ls9512 committed Mar 21, 2024
1 parent 2893033 commit 1995c2f
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 254 deletions.
100 changes: 100 additions & 0 deletions Runtime/Script/Binder/RuntimePropertyBinder(T1).cs
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;
}
}
11 changes: 11 additions & 0 deletions Runtime/Script/Binder/RuntimePropertyBinder(T1).cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions Runtime/Script/Binder/RuntimePropertyBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public class RuntimePropertyBinder : RuntimePropertyBinder<object>

public RuntimePropertyBinder(string container, string key, DataDirection direction, object target, PropertyInfo propertyInfo, FieldInfo fieldInfo)
{
Container = container;
Key = key;
Direction = direction;
Target = target;
PropertyInfo = propertyInfo;
FiledInfo = fieldInfo;

// var type = target.GetType();
if (propertyInfo != null)
{
Property = propertyInfo.Name;
Expand All @@ -27,6 +21,14 @@ public RuntimePropertyBinder(string container, string key, DataDirection directi
{
Property = fieldInfo.Name;
}

// var bindKey = type.Name + "." + Property + "." + key;
Container = container;
Key = key;
Direction = direction;
Target = target;
PropertyInfo = propertyInfo;
FiledInfo = fieldInfo;
}
}
}
98 changes: 1 addition & 97 deletions Runtime/Script/Component/PropertyBinder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Globalization;
using System.Reflection;
using UnityEngine;
using UnityEngine;

namespace Aya.DataBinding
{
Expand Down Expand Up @@ -32,97 +29,4 @@ public override void Reset()
Target = null;
}
}

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;
}
}
6 changes: 4 additions & 2 deletions Runtime/Script/UBind.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Aya.Sample;
using System;
using System.Reflection;

namespace Aya.DataBinding
Expand Down Expand Up @@ -170,7 +171,8 @@ public static RuntimePropertyBinder Bind(string key, DataDirection direction, ob

public static RuntimePropertyBinder Bind(string container, string key, DataDirection direction, object target, string propertyName)
{
var (propertyInfo, fieldInfo) = TypeCaches.GetTypePropertyOrFieldByName(target.GetType(), propertyName);
var type = target.GetType();
var (propertyInfo, fieldInfo) = TypeCaches.GetTypePropertyOrFieldByName(type, propertyName);
return Bind(container, key, direction, target, propertyInfo, fieldInfo);
}

Expand Down
Loading

0 comments on commit 1995c2f

Please sign in to comment.