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.
Since you're method naming is
ToPropertyName
, I suppose you're trying to get the typed name of some particular property of a class. Did you benchmark theExpression<Func<E, T>>
approach? The cost of creating the expression is quite larger and since your method is static, I see you're not caching the member expression as well. In other words even if the expression approach doesn't use reflection the cost can be high. See this question: How do you get a C# property name as a string with reflection? where you have another approach:You can use it to get name of property or a variable, like this:
To speed up further, you would need to cache the member info. If what you have is absolutely
Func<E, T>
then your approach suits. Also see this: lambda expression based reflection vs normal reflectionA related question: Get all the property names and corresponding values into a dictionary
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.Reflection
such asType
orPropertyInfo
are used together with types from theSystem.Linq.Expressions
namespace. 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.