Skip to content

Commit

Permalink
Remove System.Linq.Expressions usages in ReflectionClrPropertyInfo (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJul authored Aug 1, 2024
1 parent b1489aa commit 8493faf
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions src/Avalonia.Base/Data/Core/ClrPropertyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Avalonia.Data.Core
Expand Down Expand Up @@ -40,35 +39,19 @@ public void Set(object target, object? value)

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)
{

}
}
}

0 comments on commit 8493faf

Please sign in to comment.