|
|
@ -1,5 +1,4 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.Linq.Expressions; |
|
|
|
|
|
using System.Reflection; |
|
|
using System.Reflection; |
|
|
|
|
|
|
|
|
namespace Avalonia.Data.Core |
|
|
namespace Avalonia.Data.Core |
|
|
@ -40,35 +39,19 @@ namespace Avalonia.Data.Core |
|
|
|
|
|
|
|
|
public class ReflectionClrPropertyInfo : ClrPropertyInfo |
|
|
public class ReflectionClrPropertyInfo : ClrPropertyInfo |
|
|
{ |
|
|
{ |
|
|
static Action<object, object?>? CreateSetter(PropertyInfo info) |
|
|
private static Action<object, object?>? CreateSetter(PropertyInfo info) |
|
|
{ |
|
|
=> info.SetMethod is { } setMethod ? |
|
|
if (info.SetMethod == null) |
|
|
(target, value) => setMethod.Invoke(target, [value]) : |
|
|
return null; |
|
|
null; |
|
|
var target = Expression.Parameter(typeof(object), "target"); |
|
|
|
|
|
var value = Expression.Parameter(typeof(object), "value"); |
|
|
private static Func<object, object?>? CreateGetter(PropertyInfo info) |
|
|
return Expression.Lambda<Action<object, object?>>( |
|
|
=> info.GetMethod is { } getMethod ? |
|
|
Expression.Call(Expression.Convert(target, info.DeclaringType!), info.SetMethod, |
|
|
target => getMethod.Invoke(target, []) : |
|
|
Expression.Convert(value, info.SetMethod.GetParameters()[0].ParameterType)), |
|
|
null; |
|
|
target, value) |
|
|
|
|
|
.Compile(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Func<object, object>? CreateGetter(PropertyInfo info) |
|
|
|
|
|
{ |
|
|
|
|
|
if (info.GetMethod == null) |
|
|
|
|
|
return null; |
|
|
|
|
|
var target = Expression.Parameter(typeof(object), "target"); |
|
|
|
|
|
return Expression.Lambda<Func<object, object>>( |
|
|
|
|
|
Expression.Convert(Expression.Call(Expression.Convert(target, info.DeclaringType!), info.GetMethod), |
|
|
|
|
|
typeof(object)), |
|
|
|
|
|
target) |
|
|
|
|
|
.Compile(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ReflectionClrPropertyInfo(PropertyInfo info) : base(info.Name, |
|
|
public ReflectionClrPropertyInfo(PropertyInfo info) : base(info.Name, |
|
|
CreateGetter(info), CreateSetter(info), info.PropertyType) |
|
|
CreateGetter(info), CreateSetter(info), info.PropertyType) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|