mardi 22 novembre 2016

Call metthod with Expression through reflection

I call method with Expression, which return last record in table :

   public T FindLast<TKey>(Expression<Func<T,TKey>> specification = null)
{
    return specification == null
        ? Set().LastOrDefault()
        : Set().OrderBy(specification).LastOrDefault();
}

through reflection

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

On the last line I have error message

Object of type 'System.Linq.Expressions.Expression1[System.Func2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Int32]]' cannot be converted to type 'System.Linq.Expressions.Expression1[System.Func2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Linq.Expressions.Expression1[System.Func2[RRP.Framework.Domain.Entities.ArticleAttribute,System.Int32]]]]'.





Aucun commentaire:

Enregistrer un commentaire