通过传递表达式<;Func<;T、 对象>>;

本文关键字:gt lt 对象 Func 表达式 | 更新日期: 2023-09-27 18:21:55

根据我自己的问题,我突然想到,我现在需要通过向我的方法传递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 });

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.Ddomain.Models.CL LI,System.Object]][]"的对象。

这基本上意味着我的Expression<Func<CLLI, IList<Grouping>>>不能在我的方法中使用,该方法需要Expression<Func<CLLI, object>>

如果我真的直接打电话给我的服务:

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

它有效。

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

通过传递表达式<;Func<;T、 对象>>;

问题是Expression<T>是不变的,所以即使你有一个类型T可以分配给类型U,也不意味着Expression<T>可以分配给Expression<U>。在您的情况下,TFunc<CLI, IList<Grouping>>UFunc<CLLI, 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);
}