jeudi 17 novembre 2016

Exclude generic function at runtime using PropertyInfo

lI have the following function:

private static Expression<Func<TEntity, TValue>> BuildLambda<TEntity, TValue>(string property) where TEntity : class
        {
            var param = Expression.Parameter(typeof (TEntity), "e");
            var prop = Expression.PropertyOrField(param, property);
            return Expression.Lambda<Func<TEntity, TValue>>(prop, param); 
        }

I am playing around with reflections:

How can exclude this function at runtime with a dynamic TEntity and TValue. Is it even possible?

Example:

public static void ValidateEntity<TEntity>(AbstractValidator<TEntity> validator,PropertyRule propRule) where TEntity : class, new()
        {
            var propertyName = propRule.Expression.GetMember().Name;
            var propType = typeof(TEntity).GetProperty(propertyName); 
            //trying to exclude BuildLamdba don't know TValue only have PropertyInfo
            //var expresssion = BuildLambda<TEntity, IEnumerable<TValue>>(propertyName); 

....
}





Aucun commentaire:

Enregistrer un commentaire