C# 通过反射调用带表达式的Method

C# 通过反射调用带表达式的Method,c#,entity-framework,generics,reflection,C#,Entity Framework,Generics,Reflection,我使用表达式调用方法,它返回表中的最后一条记录: public T FindLast<TKey>(Expression<Func<T,TKey>> specification = null) { return specification == null ? Set().LastOrDefault() : Set().OrderBy(specification).LastOrDefault(); } public

我使用表达式调用方法,它返回表中的最后一条记录:

   public T FindLast<TKey>(Expression<Func<T,TKey>> specification = null)
{
    return specification == null
        ? Set().LastOrDefault()
        : Set().OrderBy(specification).LastOrDefault();
}
public T FindLast(表达式规范=null)
{
返回规范==null
?Set().LastOrDefault()
:Set().OrderBy(规范).LastOrDefault();
}
反省

 var methodCreateReadRepositoryAttr = (entityMetadata.GetEntityAttributeType() != null) ? 
typeof(IRepositoryFactory).GetMethod("CreateReadRepository").MakeGenericMethod(entityMetadata.GetEntityAttributeType()) : null;

    var methodEntityGet3 = attributeReadRepository.GetType().GetMethod("FindLast");
var closedGenericMethod = methodEntity3.MakeGenericMethod(new Type[] { typeof(Expression<Func<ArticleAttribute,int>>) };

Expression <Func<ArticleAttribute, int>> articleReturnExpression = e => e.ArticleAttributeID;   

 var fromRepo3 = closedGenericMethod.Invoke(attributeReadRepository, new object[] {articleReturnExpression});
var methodCreateReadRepositoryAttr=(entityMetadata.GetEntityAttributeType()!=null)?
typeof(IRepositoryFactory).GetMethod(“CreateReadRepository”).MakeGenericMethod(entityMetadata.GetEntityAttributeType()):null;
var methodEntityGet3=attributeReadRepository.GetType().GetMethod(“FindLast”);
var closedGenericMethod=methodEntity3.MakeGenericMethod(新类型[]{typeof(表达式)};
表达式articleReturnExpression=e=>e.ArticleAttributeID;
var fromRepo3=closedGenericMethod.Invoke(AttributeRepository,新对象[]{articleReturnExpression});
在最后一行,我有一条错误消息

“System.Linq.Expressions.Expression
1[System.Func
2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Int32]]”类型的对象无法转换为“System.Linq.Expressions.Expression
1[System.Func
2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Linq.Expressions.Expression
1[System.Func
2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Int32]]]'


您正在混合泛型参数类型和方法参数类型

方法的唯一泛型参数

public T FindLast<TKey>(Expression<Func<T,TKey>> specification = null)
应该是

var closedGenericMethod = methodEntity3.MakeGenericMethod(
    new Type[] { typeof(int) });

这有什么不同?这是apear的下一个问题。对不起!请提供一个答案,以便您可以得到一个答案,我们可以证明它一直都是有效的。它不能帮助任何人一次解决一个小问题,而我们可以通过一个完整的示例给出完整的解决方案。
var closedGenericMethod = methodEntity3.MakeGenericMethod(
    new Type[] { typeof(int) });