根据我自己的问题,我现在需要通过将 an 传递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.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]][]'。
这基本上意味着 myExpression<Func<CLLI, IList<Grouping>>>
不能在我的方法中使用,它需要一个Expression<Func<CLLI, object>>
.
如果我真的直接调用我的服务:
IService<CLLI> clliService = new Service<CLLI>();
clliService.List(clli => clli.Groupings);
有用。
我将如何解决这个问题?IList 不是对象吗?