Browse Source

Remove System.Linq.Expressions usages in ReflectionClrPropertyInfo (#16568)

pull/16575/head
Julien Lebosquain 2 years ago
committed by GitHub
parent
commit
8493faf9c0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 35
      src/Avalonia.Base/Data/Core/ClrPropertyInfo.cs

35
src/Avalonia.Base/Data/Core/ClrPropertyInfo.cs

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

Loading…
Cancel
Save