我的问题是:我在编译时不知道我的类型,因为其中一些是在运行时发出的。任何人都知道一个实现非通用的IEnumerable Linq Helper扩展方法的库吗?
例如:
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