-->

Generic method to get property values with Linq Ex

2019-07-26 20:30发布

问题:

Dear Gods of Reflection

I would like to have a generic GetValue<TEntity, T> method that can return the following property values given the following User class:

public class User  
{
   public int Id { get; set; }
   public int ClientId { get; set; }
   public string UserName { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string MobileNumber { get; set; }   
   public bool IsActive { get; set; }

   public Client Client { get; set; }
   public List<Package> Packages { get; set; }

 }

Example usage of what GetValue<TEntity, T> should be able to do:

  var firstName = dataCollector.GetValue<User, string>(x => x.FirstName);
  var client = dataCollector.GetValue<User, Client>(x => x.Client);
  var packages = dataCollector.GetValue<User, List<Package>>(x => x.Packages);

  var packageFirst = dataCollector.GetValue<User, Package>(x => x.Packages[0]);
  var packageName = dataCollector.GetValue<User, string>(x => x.Packages[0].Name);
  var clientName = dataCollector.GetValue<User, string>(x => x.Client.Name);

So far I have the following method which works for the first 3 scenarios:

 public T GetValue<TEntity, T>(Expression<Func<TEntity, T>> propertyExpression) where TEntity : class
 {
    var response = _responses.FirstOrDefault(p => p.GetType() == typeof(TEntity)) as TEntity;
    if (response != null)
    {
       var expr = (MemberExpression)propertyExpression.Body;
       var prop = (PropertyInfo)expr.Member;
       return (T)prop.GetValue(response);
    }
    return default(T);
  }

But it does not work for the last 3 scenarios:

  var packageFirst = dataCollector.GetValue<User, Package>(x => x.Packages[0]);

Throws: Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'.

  var packageName = dataCollector.GetValue<User, string>(x => x.Packages[0].Name);

Throws: Object does not match target type.

  var clientName = dataCollector.GetValue<User, string>(x => x.Client.Name);

Throws: Object does not match target type.

What changes do I need to make to the method?

I shall now sacrifice a USB flash drive whilst awaiting your answers :)

回答1:

The problem is here:

if (response != null)
{
   var expr = (MemberExpression)propertyExpression.Body;
   var prop = (PropertyInfo)expr.Member;
   return (T)prop.GetValue(response);
}

This only works if your expression references a property directly, otherwise propertyExpression.Body will not be a MemberExpression and you'll get a run-time cast error. The three that don't work do not reference a property directly - the first two reverence a method on top of a property (the indexer) and the last references a nested property.

Since all you want is the value of the expression, though, I think you can just do:

if (response != null)
{
   Func<TEntity, T> func = propertyExpression.Compile();  
   return func(response);
}

If you intended to do other things with the expression (like get the name of the property), then you'll need to decide if you want to support expressions that don't reference a property directly and add handlers for that.



回答2:

You can do it simply by Executing your Lambda expression:

        if (response != null)
        {
            return propertyExpression.Compile().Invoke(response);
        }