gpt4 book ai didi

c# - 使用表达式树为新类创建 Lambda 表达式选择器

转载 作者:太空狗 更新时间:2023-10-30 00:48:21 24 4
gpt4 key购买 nike

相关于:

Create Expression Tree For Selector

Create a Lambda Expression With 3 conditions

Convert Contains To Expression Tree

Convert List.Contains to Expression Tree

我想使用 Expression Tree 为新类创建 Selector 表达式。请考虑这段代码:

s => new Allocation
{
Id = s.Id,
UnitName = s.UnitName,
Address = s.NewAddress,
Tel = s.NewTel
}

我有一个大类 (MyClass),我想选择它的一些属性。但我想动态创建它。我该怎么做?

谢谢

最佳答案

解决这个问题的方法是编写等效代码,然后反编译它。例如:

using System;
using System.Linq.Expressions;

class Program
{
static void Main()
{
ShowMeTheLambda<Foo, Allocation>(s => new Allocation
{
Id = s.Id,
UnitName = s.UnitName,
Address = s.NewAddress,
Tel = s.NewTel
});
}
static void ShowMeTheLambda<TFrom, TTo>(Expression<Func<TFrom, TTo>> lambda)
{ }
}
class Foo
{
public int Id { get; set; }
public string UnitName { get; set; }
public string NewTel { get; set; }
public string NewAddress { get; set; }
}
class Allocation
{
public int Id { get; set; }
public string UnitName { get; set; }
public string Tel { get; set; }
public string Address { get; set; }
}

现在,如果我编译它并用“反射器”反编译它,我得到:

private static void Main()
{
ParameterExpression expression;
MemberBinding[] bindings = new MemberBinding[] { Expression.Bind((MethodInfo) methodof(Allocation.set_Id), Expression.Property(expression = Expression.Parameter(typeof(Foo), "s"), (MethodInfo) methodof(Foo.get_Id))), Expression.Bind((MethodInfo) methodof(Allocation.set_UnitName), Expression.Property(expression, (MethodInfo) methodof(Foo.get_UnitName))), Expression.Bind((MethodInfo) methodof(Allocation.set_Address), Expression.Property(expression, (MethodInfo) methodof(Foo.get_NewAddress))), Expression.Bind((MethodInfo) methodof(Allocation.set_Tel), Expression.Property(expression, (MethodInfo) methodof(Foo.get_NewTel))) };
ParameterExpression[] parameters = new ParameterExpression[] { expression };
ShowMeTheLambda<Foo, Allocation>(Expression.Lambda<Func<Foo, Allocation>>(Expression.MemberInit(Expression.New(typeof(Allocation)), bindings), parameters));

}

注意:memberofmethodof 实际上并不存在于 C# 中 - 您可以通过反射手动获取方法信息,或使用 Expression.PropertyOrField。因此我们可以将其重写为:

ParameterExpression expression = Expression.Parameter(typeof(Foo), "s");
MemberBinding[] bindings = new MemberBinding[]
{
Expression.Bind(typeof(Allocation).GetProperty(nameof(Allocation.Id)), Expression.PropertyOrField(expression, nameof(Foo.Id))),
Expression.Bind(typeof(Allocation).GetProperty(nameof(Allocation.UnitName)), Expression.PropertyOrField(expression, nameof(Foo.UnitName))),
Expression.Bind(typeof(Allocation).GetProperty(nameof(Allocation.Address)), Expression.PropertyOrField(expression, nameof(Foo.NewAddress))),
Expression.Bind(typeof(Allocation).GetProperty(nameof(Allocation.Tel)), Expression.PropertyOrField(expression, nameof(Foo.NewTel))),
};
ParameterExpression[] parameters = new ParameterExpression[] { expression };
var lambda = Expression.Lambda<Func<Foo, Allocation>>(Expression.MemberInit(Expression.New(typeof(Allocation)), bindings), parameters);

关于c# - 使用表达式树为新类创建 Lambda 表达式选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47248730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com