简体   繁体   English

如何使用表达式树构建匿名对象

[英]How to build an anonymous object with expression trees

I have a class 我有一堂课

public class SomeClass
{
    public int num { get; set; }
    public string str{ get; set; }
}

I need to construct a lambda expression like: (x => new {new_num= x.num, new_str= x.str}) 我需要构造一个lambda表达式,例如: (x => new {new_num = x.num,new_str = x.str})

Properties are listed in some enumerable object (that's important). 属性在一些可枚举的对象中列出(这很重要)。 Let's say List<KeyValuePair<string, string>> 假设List<KeyValuePair<string, string>>

My code is: 我的代码是:

// Test properties list
var listOfParams = new List<KeyValuePair<string, string>>();
listOfParams.Add(new KeyValuePair<string, string>("new_num","num"));
listOfParams.Add(new KeyValuePair<string, string>("new_str","str"));

// SomeClass as particular test case
var tModelType = typeof(SomeClass); 

ParameterExpression pe = Expression.Parameter(tModelType, "m");
NewExpression ne = Expression.New(typeof(object)); //<-- trying to emulate `new object{}`

List<Expression> lInit = new List<Expression>(); 

PropertyInfo pi;
string sAnonymousProperty;

Type propertyType;
foreach(var paramElem in listOfParams)
{
   pi = tModelType.GetProperty(paramElem.Value);
   propertyType = pi.PropertyType;
   sAnonymousProperty = paramElem.Value;

   Expression bind = ??Expression??  // <-- here I need to build binding expression

   lInit.Add( bind );
}

ListInitExpression lie = Expression.ListInit(ne, lInit);

LambdaExpression leResult = Expression.Lambda(lie, pe);

I can't figure out binding expression, because it needs MemberInfo, which anonymous object hasn't got. 我不知道绑定表达式,因为它需要MemberInfo,而匿名对象还没有。 Also I can't walkaround it using new { new_num = 0, new_str = "" }.GetType() since properties are unknown and come as some array as a parameter. 另外,我无法使用new { new_num = 0, new_str = "" }.GetType()遍历它new { new_num = 0, new_str = "" }.GetType()因为属性是未知的,并且以某些数组作为参数。

Maybe my approach is invalid at all and someone will suggest another way, but main idea is to obtain lambda-expression/"Func"-delegate that creates anonymous object with dynamic remapped properties. 也许我的方法根本无效,有人会提出另一种方法,但是主要思想是获得lambda-expression /“ Func” -delegate,该代理创建具有动态重映射属性的匿名对象。

Any help is appreciated. 任何帮助表示赞赏。

The approach is basically invalid, because there's no appropriate type to refer to. 该方法基本上无效的,因为没有合适的类型可以引用。 An anonymous type is just a normal type as far as the CLR is concerned - the compiler just generates the type for you. 就CLR而言,匿名类型只是普通类型-编译器只是为您生成该类型。 (In the Microsoft implementation it actually generates a generic type, which is used for every anonymous type with the same property names used in the same order.) (在Microsoft实现中,它实际上会生成一个泛型类型,该泛型类型用于具有相同属性名称且顺序相同的每个匿名类型。)

So in order to emulate this, you'd need to create your own type as well. 因此,为了模拟这一点,您还需要创建自己的类型。 Alternatively, you could use Tuple<T1, T2> etc - using the right tuple type based on the number of properties you have. 或者,您可以使用Tuple<T1, T2>等-根据所拥有的属性数量使用正确的元组类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM