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*/;
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();
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>
The answer is effectively the same as this other SO question/answer on INotifyPropertyChanged by Phil.