动态创建匿名类型时“参数表达式无效”

时间:2018-08-18 15:36:47

标签: linq expression-trees typebuilder

我正在创建一个表达式树生成器以返回自定义匿名类型。我首先使用离散类型尝试了该方法,但效果很好,但是使用TypeBuilder在运行时构建类型并将该类型传递给表达式树失败

'Argument expression is not valid'

这是我使用的代码:

我用来创建匿名类型的此方法

private Type CreateAnonymousType(Dictionary<string, Type> properties)
    {
      AssemblyName dynamicAssemblyName = new AssemblyName("MyAssembly");
      AssemblyBuilder dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("MyAssembly");

      TypeBuilder dynamicAnonymousType = dynamicModule.DefineType("ReturnType", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass);
      foreach (var p in properties)
      {
        dynamicAnonymousType.DefineField(p.Key, p.Value, FieldAttributes.Public);
      }

      return dynamicAnonymousType.CreateType();
    }

这是我创建表达式树的方式

var cars = new List<Car>();
      for (int i = 0; i < 10; i++)
      {
        cars.Add(new Car { Id = i, Name = "Car " + i, Age = 2010 + i });
      }


      IQueryable<Car> allCars = cars.AsQueryable();

      var properties = new Dictionary<string, Type>
      {
        { "Id", typeof(int) },
        { "Name", typeof(string) }
      };

      ParameterExpression x = Expression.Parameter(typeof(Car), "x");
      var listMembers = properties.Select(p => Expression.Property(x, p.Key));

      var returnType = CreateAnonymousType(properties);
      object destObject = Activator.CreateInstance(returnType);


      var listBind = listMembers.Select(p => Expression.Bind(returnType.GetField(p.Member.Name), p));

      var result = Expression.New(returnType);
      var initExp = Expression.MemberInit(result, listBind.ToArray());


      var call = Expression.Call(typeof(Queryable), "Select",
          new Type[] {
            typeof(Car),
            returnType
        }
        , Expression.Constant(allCars)
        , Expression.Lambda(initExp, x));

      var qResult = allCars.Provider.CreateQuery<IdName>(call);

      foreach (var car in qResult)
      {
        Console.WriteLine(car.Id + " - " + car.Name);
      }

执行CreateQuery方法时发生错误

1 个答案:

答案 0 :(得分:0)

这是因为call返回的是动态创建的ReturnType,而不是IdName,因此是例外。另外,您不能将诸如ReturnType之类的动态类型作为通用类型参数,因为编译器对此一无所知,因此您应该使用dynamic以便在运行时解析类型:

var qResult = allCars.Provider.CreateQuery<dynamic>(call);