I have the following code
Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null;
and want to tranform it to
Expression<Func<PersistentAttributeInfo, bool>> expression = info => info.Owner== null;
PersistentAttributeInfo is only known开发者_StackOverflow中文版 at runtime though
Is it possible?
If PersistentAttributeInfo is only known at runtime, you obviously cannot write the lambda statically and have the compiler do the heavy lifting for you. You'll have to create a new one from scratch:
Type persistentAttributeInfoType = [TypeYouKnowAtRuntime];
ParameterExpression parameter = Expression.Parameter(persistentAttributeInfoType, "info");
LambdaExpression lambda = Expression.Lambda(
typeof(Func<,>).MakeGenericType(persistentAttributeInfoType, typeof(bool)),
Expression.Equal(Expression.Property(parameter, "Owner"), Expression.Constant(null)),
parameter);
You can invoke lambda.Compile() to return a Delegate that is analogous to the transformed lambda expression in your example (though of course untyped).
精彩评论