Use reflection to assign value of Func pr

2019-05-14 13:44发布

问题:

If I have a class Product:

public class Product
{
    public string Title { get; set; }
    public string Make { get; set; }
    public Decimal Price { get; set; } //(Edit) - Added non-string
}

And I have a property in another class declared as:

Func<Product, object> SortBy { get; set; }

I can set SortBy using:

SortBy = p => p.Title;

But how would I, using reflection, make the same assignment if I had the property name for SortBy stored as a string e.g.

string sortField = "Title";

SortBy = /*Some reflection using sortField*/;

回答1:

You need to use expression trees to create a new method at runtime:

var p = Expression.Parameter(typeof(Product));
SortBy = Expression.Lambda<Func<Product, object>>(
    Expression.Property(p, sortField),
    p
).Compile();

To work with value types, you'll need to insert a cast:

var p = Expression.Parameter(typeof(Product));
SortBy = Expression.Lambda<Func<Product, object>>( 
    Expression.TypeAs(Expression.Property(p, sortField), typeof(object)), 
    p
).Compile();


回答2:

To make it work with decimal and other value types, you can use generics:

static void SetSortBy<T>(string sortField) {
    var m = typeof(Product).GetProperty(sortField).GetGetMethod();
    var d = Delegate.CreateDelegate(typeof(Func<Product, T>), m) 
            as Func<Product, T>;
    SortBy = product => d(product);
}

...

SetSortBy<decimal>("Price");
SetSortBy<object>("Title"); // or <string>


回答3:

The answer is effectively the same as this other SO question/answer on INotifyPropertyChanged by Phil.