繁体   English   中英

通过传递一个表达式来调用使用反射的方法 <Func<T, object> &gt;

[英]Invoke a method using reflection by passing an Expression<Func<T, object>>

根据我自己的问题 ,我想到现在需要通过将Expression<Func<T, object>>传递给我的方法来调用相同的服务和相同的方法。 这是方法定义:

IList<T> List(params Expression<Func<T, object>>[] includeProperties);

这是我现在拥有的代码:

  //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 });

包含成功创建了我的lambda表达式(Param_0 => Param_0.Groupings),我相信这将是我的Expression<Func<T, object>> 但是,由于Param_0.Groupings实际上是一个IList,因此出现异常:

类型为'System.Linq.Expressions.Expression 1[System.Func 2 [Permet.BackEnd.ETL.Domain.Models.CLLI,System.Collections.Generic.IList 1[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]] []'。

这基本上意味着我的Expression<Func<CLLI, IList<Grouping>>>无法在我的需要Expression<Func<CLLI, object>>

如果我实际上直接致电我的服务:

IService<CLLI> clliService = new Service<CLLI>();
clliService.List(clli => clli.Groupings);

有用。

我将如何解决这个问题? IList不是对象吗?

问题在于Expression<T>是不变的,因此即使您具有可以分配给类型U T类型,也不意味着可以将Expression<T>分配给Expression<U> 在您的情况下, TFunc<CLI, IList<Grouping>>UFunc<CLLI, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM