vendredi 13 mai 2016

Object of type 'System.Reflection.RuntimeMethodInfo' cannot be converted to type 'System.Linq.Expressions.Expression'

I have a program through Which i want to give Lambda expression Dynamic to Linq but it throws Exception

Object of type 'System.Reflection.RuntimeMethodInfo' cannot be converted to type 'System.Linq.Expressions.Expression'

here is My code

using (var uu = new DALLinqLibrary.UnitOfWork<test1Entities>(t))
{
            var genericGet = uu.GetType().GetMethod("GetRepository").MakeGenericMethod(tt.GetType());

            var Createmap = uu.GetType().GetMethod("CreateMap").MakeGenericMethod(tt.GetType());

            var repository = genericGet.Invoke(uu, null);

            var Find = repository.GetType().GetMethod("Find");

            var records = Find.Invoke(repository, new object[] { Createmap });
}

Createmap Function Implementation is

 public Expression<Func<TEntity, bool>> CreateMap<TEntity>() where TEntity : class
     {
         PropertyInfo[] props = typeof(TEntity).GetProperties();
         foreach (PropertyInfo prop in props)
         {
             var displayAttribute = prop.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DisplayNameAttribute)) as DisplayNameAttribute;
             if (displayAttribute != null)
             {
                 var parameterExpression = Expression.Parameter(typeof(TEntity), "x");
                 var memberExpression = Expression.PropertyOrField(parameterExpression, prop.Name);
                 var memberExpressionConversion = Expression.Convert(memberExpression, typeof(object));
                 var lambda = Expression.Lambda<Func<TEntity, bool>>(memberExpressionConversion, parameterExpression);
                 return lambda;
             }
         }

         return null;
     }

and GetRepository implementation is

 public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class 

    {

        if (_repositories.Keys.Contains(typeof(TEntity)))
        {
            return _repositories[typeof(TEntity)] as IRepository<TEntity>;
        }

        var repository = new Repository<TEntity>(_context);

        _repositories.Add(typeof(TEntity), repository);

        return repository;
    }

it throws Exception on This line

var records = Find.Invoke(repository, new object[] { Createmap });

any Idea?





Aucun commentaire:

Enregistrer un commentaire