Enumerable.Select的表达式树

时间:2013-06-14 14:00:28

标签: c# expression-trees

编辑我想我可以提出更好的要求(在这种情况下根本不需要代码)。所以问题一般是:在运行时创建(Select<TEntity,TResult>时,如何使用表达式树来构建对我的情况下的泛型方法TResult的调用?忽略下面的所有代码和文本,这是问题的不明确版本,让它不会混淆那些回答。

我需要一个例子来说明如何为“选择”调用构建表达式树。问题是我在编译时不知道结果类型(它可以由用户在运行时通过某些GUI定义)。这里有一些我正在尝试这样做的代码(记住我不能使用任何泛型)

class Pet {
    public int Id { get; set; }
    public string Name { get; set; }
}

在Main中使用此类:

List<Pet> list = new List<Pet>();                
Expression eList = Expression.Constant(list);
ParameterExpression pe = Expression.Parameter(typeof(Pet), "p");
MethodInfo method = typeof(Program).GetMethod("CreateObject", BindingFlags.Static | BindingFlags.NonPublic);
var properties = typeof(Pet).GetProperties().Where(pi => pi.Name == "Name"); //will be defined by user
Expression selectorBody = Expression.Call(method, Expression.Constant(properties));
Expression selector = Expression.Lambda(selectorBody, pe);
Expression res = Expression.Call(typeof(Enumerable), "Select", new[] { typeof(Pet), CreateType(properties) }, eList, selector);

我要做的是在运行时使用Reflection.Emit创建一个Type(上面的代码中的方法“CreateType”,它在我的一些项目中使用并且看起来很好)并以某种方式传递它以调用“Select” “,但我得到例外:

  

没有泛型方法'选择'类型'System.Linq.Enumerable'与提供的类型参数和参数兼容。

有什么建议吗?

更新我真正的问题是为运行时类型的“加入”调用构建一个表达式树,这更复杂,所以我决定询问“选择”,因为我有相同的最后一行中的异常(Expression.Call(...))

Upd2 包含了我的帮助方法和编辑过的主代码,但这不是主要问题。

static Type CreateType(IEnumerable<PropertyInfo> properties) {
        TypeBuilder typeBuilder = CreateTypeBuilder("ResultDynamicAssembly", "ResultModule", "ResultType");
        foreach (PropertyInfo propertyInfo in properties) {
            CreateAutoImplementedProperty(typeBuilder, propertyInfo.Name, propertyInfo.PropertyType);
        }
        return typeBuilder.CreateType();
    }

    static object CreateObject(IEnumerable<PropertyInfo> properties) {
        Type type = CreateType(properties); 
        return Activator.CreateInstance(type);
    }

2 个答案:

答案 0 :(得分:4)

您可以通过从等式中删除动态类型来简化此问题。您可以使用下面的代码重现相同的问题,这完全相同,但没有动态类型。

static class Program
{
    private static void Main(string[] args)
    {
        var list = new List<Pet>();
        var eList = Expression.Constant(list);
        var pe = Expression.Parameter(typeof(Pet), "p");
        var method = typeof(Program).GetMethod("CreateObject", BindingFlags.Static | BindingFlags.NonPublic);
        var properties = typeof(Pet).GetProperties().Where(pi => pi.Name == "Name"); //will be defined by user
        var selectorBody = Expression.Call(method, Expression.Constant(properties));
        var selector = Expression.Lambda(selectorBody, pe);
        var res = Expression.Call(typeof(Enumerable), "Select", new[] { typeof(Pet), CreateType(properties) }, eList, selector);
    }

    private static Type CreateType(IEnumerable<PropertyInfo> properties)
    {
        return typeof (DynamicType);
    }

    private static object CreateObject(IEnumerable<PropertyInfo> properties)
    {
        var type = CreateType(properties);
        return  Activator.CreateInstance(type);
    }

    class Pet
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class DynamicType
    {
        public string Name { get; set; }
    }
}

所以问题是CreateObject的方法签名。由于其返回类型不是动态类型,因此lambda无效。您可以通过更改CreateObject的类型来查看此内容。

    // this works fine
    private static DynamicType CreateObject(IEnumerable<PropertyInfo> properties)
    {
        var type = CreateType(properties);
        return  (DynamicType) Activator.CreateInstance(type);
    }

由于您正在处理动态类型,因此需要构建一个表达式以将CreateObject的结果强制转换为动态类型。尝试使用这样的东西:

        // create dynamic type
        var properties = typeof(Pet).GetProperties().Where(pi => pi.Name == "Name"); //will be defined by user
        var dynamicType = CreateType(properties);

        // build expression
        var list = new List<Pet>();
        var eList = Expression.Constant(list);
        var pe = Expression.Parameter(typeof(Pet), "p");
        var createObjectMethod = typeof(Program).GetMethod("CreateObject", BindingFlags.Static | BindingFlags.NonPublic);
        var createObjectCall = Expression.Call(createObjectMethod, Expression.Constant(properties));
        var castExpression = Expression.Convert(createObjectCall, dynamicType);
        var selectorExpression = Expression.Lambda(castExpression, pe);
        var res = Expression.Call(typeof(Enumerable), "Select", new[] { typeof(Pet), dynamicType }, eList, selectorExpression);

答案 1 :(得分:-1)

为每种类型创建工厂。你知道它的来源是什么,所以它非常简单:

interface PetResultFactory<T>
{
    string TypeName {get; }
    List<T> Transform(IEnumerable<Pet> pets);
}

您必须在容器中注册所有可能的类型,并在UI上为用户显示已注册的类型。当用户选择一个,你知道如何创建该类型,因为它在工厂中描述。