如何使用使用动态类型的Linq扩展方法?
我的问题是:我在编译时不知道我的类型,因为其中一些类型是在运行时发出的。有人知道实现非泛型IEnumerable Linq辅助扩展方法的库吗 例如:如何使用使用动态类型的Linq扩展方法?,linq,extension-methods,ienumerable,helper,Linq,Extension Methods,Ienumerable,Helper,我的问题是:我在编译时不知道我的类型,因为其中一些类型是在运行时发出的。有人知道实现非泛型IEnumerable Linq辅助扩展方法的库吗 例如: public static IEnumerable Join( this IEnumerable source, Type outerType, Type innerType, IEnumerable inner, LambdaExpression outerKeySelector, Lam
public static IEnumerable Join(
this IEnumerable source,
Type outerType,
Type innerType,
IEnumerable inner,
LambdaExpression outerKeySelector,
LambdaExpression innerKeySelector,
LambdaExpression resultSelector)
{
var method =
typeof(Enumerable).GetMethods()
.Single(x => x.Name == "Join" && x.GetParameters().Length == 5);
return
method.MakeGenericMethod(
outerType,
innerType,
innerKeySelector.ReturnType,
resultSelector.ReturnType)
.Invoke(
null,
new object[] { source, inner, outerKeySelector.Compile(),
innerKeySelector.Compile(),
resultSelector.Compile() }) as IEnumerable;
}
编辑:
以下是我构造lambda表达式的方式:
var property = myType.GetProperty("SomeProperty");
var p = Expression.Parameter(myType, "p");
var lambda = Expression.Lambda(Expression.MakeMemberAccess(p, property), p);
所以,我的lambda是
p => p.SomeProperty
如果不知道类型,您如何构建lambda表达式?类型是发出的(动态程序集),因此我在运行时知道它们,但在编译时不知道。因此lambda表达式也是发出的?是的,它们是在运行时构造的。我更新了问题以包含这些信息。那么,在什么情况下,您实际使用发出的类型呢?还是一切都是动态的?