[英]Invoke a method using reflection by passing an Expression<Func<T, object>>
Following my own question it has occured to me that I now need to invoke the same service and the same method by passing an Expression<Func<T, object>>
to my method. 根据我自己的问题 ,我想到现在需要通过将
Expression<Func<T, object>>
传递给我的方法来调用相同的服务和相同的方法。 Here is the method definition: 这是方法定义:
IList<T> List(params Expression<Func<T, object>>[] includeProperties);
Here is the code I have right now: 这是我现在拥有的代码:
//Get generic type
var entityRelationType = typeof(Applicant).Assembly.GetType(string.Format("Permet.BackEnd.ETL.Domain.Models.{0}", tableRelation.RelationEntityName));
//create service that will receive the generic type
var definitionIService = typeof(IService<>).MakeGenericType(entityRelationType);
//instantiate the service using Unity (todo: fix singleton)
var serviceInstance = UnitySingleton.Container.Resolve(definitionIService, "");
//create the argument for the method that we invoke
var paramsType =
typeof(Expression<>).MakeGenericType(typeof(Func<,>)
.MakeGenericType(entityRelationType, typeof(object))).MakeArrayType();
#region Get Dynamic Data
ParameterExpression relationParameter = Expression.Parameter(entityRelationType, "");
//build the parameter that we want to pass to the method (Expression<Func<T, object>>
var include =
Expression.Lambda(
Expression.Property(relationParameter, tableRelation.NaviguationProprietyName),
relationParameter
);
dynamic datas = constructedIService
.GetMethod("List", new Type[] { paramsType }).Invoke(serviceInstance, new object[] { include });
The include successfully creates my lambda expression (Param_0 => Param_0.Groupings) which I believed would be my Expression<Func<T, object>>
. 包含成功创建了我的lambda表达式(Param_0 => Param_0.Groupings),我相信这将是我的
Expression<Func<T, object>>
。 However, since Param_0.Groupings is actually an IList, I get an exception: 但是,由于Param_0.Groupings实际上是一个IList,因此出现异常:
Object of type 'System.Linq.Expressions.Expression
1[System.Func
2[Permet.BackEnd.ETL.Domain.Models.CLLI,System.Collections.Generic.IList1[Permet.BackEnd.ETL.Domain.Models.Grouping]]]' cannot be converted to type 'System.Linq.Expressions.Expression
1[System.Func`2[Permet.BackEnd.ETL.Domain.Models.CLLI,System.Object]][]'.类型为'System.Linq.Expressions.Expression
1[System.Func
2 [Permet.BackEnd.ETL.Domain.Models.CLLI,System.Collections.Generic.IList1[Permet.BackEnd.ETL.Domain.Models.Grouping]]]' cannot be converted to type 'System.Linq.Expressions.Expression
1 [System.Func`2 [Permet.BackEnd.ETL.Domain.Models.CLLI,System.Object]] []'。
Which basically means that my Expression<Func<CLLI, IList<Grouping>>>
cannot be use in my method which expects a Expression<Func<CLLI, object>>
. 这基本上意味着我的
Expression<Func<CLLI, IList<Grouping>>>
无法在我的需要Expression<Func<CLLI, object>>
。
If I actually call my service directly doing: 如果我实际上直接致电我的服务:
IService<CLLI> clliService = new Service<CLLI>();
clliService.List(clli => clli.Groupings);
It works. 有用。
How would I go around this issue? 我将如何解决这个问题? Isn't an IList an object?
IList不是对象吗?
The problem is that Expression<T>
is invariant, so even if you have a type T
which can be assigned to type U
, that doesn't mean that Expression<T>
can be assigned to Expression<U>
. 问题在于
Expression<T>
是不变的,因此即使您具有可以分配给类型U
T
类型,也不意味着可以将Expression<T>
分配给Expression<U>
。 In your case, T
is Func<CLI, IList<Grouping>>
and U
is Func<CLLI, object>
. 在您的情况下,
T
是Func<CLI, IList<Grouping>>
而U
是Func<CLLI, object>
。
I think the only solution is to create a function to wrap a given expression in an Expression<Func<T, object>>
which delegates to the inner expression and casts the result to object
: 我认为唯一的解决方案是创建一个函数,将给定的表达式包装在
Expression<Func<T, object>>
,该Expression<Func<T, object>>
委托内部表达式并将结果转换为object
:
public static Expression<Func<T, object>> ConvertResult<T, TOut>(Expression<Func<T, TOut>> expr)
{
var paramExpr = Expression.Parameter(typeof(T));
var invokeExpr = Expression.Invoke(expr, paramExpr);
var castExpr = Expression.Convert(invokeExpr, typeof(object));
return Expression.Lambda<Func<T, object>>(castExpr, paramExpr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.