I have the following extension method:
public static string ToPropertyName<T,E>(this Expression<Func<E, T>> propertyExpression)
{
if (propertyExpression == null)
return null;
string propName;
MemberExpression propRef = (propertyExpression.Body as MemberExpression);
UnaryExpression propVal = null;
// -- handle ref types
if (propRef != null)
propName = propRef.Member.Name;
else
{
// -- handle value types
propVal = propertyExpression.Body as UnaryExpression;
if (propVal == null)
throw new ArgumentException("The property parameter does not point to a property", "property");
propName = ((MemberExpression)propVal.Operand).Member.Name;
}
return propName;
}
I use linq expression when passing property names instead of strings to provide strong typing and I use this function to retrieving the name of the property as a string. Does this method use reflection?
My reason for asking is this method is used quite a lot in our code and I want it to be reasonably fast enough.
As far as I know, reflection is not involved in the sense that some kind of dynamic type introspection happens behind the scenes. However, types from the
System.Reflectionsuch asTypeorPropertyInfoare used together with types from theSystem.Linq.Expressionsnamespace. They are used by the compiler only to describe anyFunc<T,E>passed to your method as an abstract syntax tree (AST). Since this transformation from aFunc<T,E>to an expression tree is done by the compiler, and not at run-time, only the lambda’s static aspects are described.Remember though that building this expression tree (complex object graph) from a lambda at run-time might take somewhat longer than simply passing a property name string (single object), simply because more objects need to be instantiated (the number depends on the complexity of the lambda passed to your method), but again, no dynamic type inspection à la
someObject.GetType()is involved.Example:
This MSDN article shows that the following lambda expression:
is transformed to something like this by the compiler:
Beyond this, nothing else happens. So this is the object graph that might then be passed into your method.