I’m trying to learn expressions, and I cannot find how to get the member type of a property in a expression. For example, consider this method:
static IEnumerable<Y> Filter<X,Y>(this IEnumerable<Person> p, Expression<Func<Person,X>> select, Expression<Func<X, Y>> format)
{
foreach (var item in p)
{
// member name
var m = ((MemberExpression)select.Body).Member;
// member attributes
var attributes = m.GetCustomAttributes(false);
// member type?
var a = select.Compile().Invoke(item);
var b = format.Compile().Invoke(a);
m.ToString();
yield return b;
}
}
It doesn’t do anything interesting, it is just for trying. If I do:
String y = _persons.Filter(p => p.DateOfBirth, d => d.ToString("yyyy")).ToArray().Single();
I can get in Y the year of the person in the collection. I want to know the type of “p.DateOfBirth” in the “Filter” method. How could I do that without using reflection?
Regards.
Assuming I’ve understood you correctly, you just need the
Expression.Typeproperty: