-->

Is reflection used when retrieving information fro

2020-07-26 02:31发布

问题:

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.

回答1:

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 as Type or PropertyInfo are used together with types from the System.Linq.Expressions namespace. They are used by the compiler only to describe any Func<T,E> passed to your method as an abstract syntax tree (AST). Since this transformation from a Func<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:

Expression<Func<int, bool>> lambda1 = num => num < 5;

is transformed to something like this by the compiler:

ParameterExpression numParam = Expression.Parameter(typeof(int), "num");
ConstantExpression five = Expression.Constant(5, typeof(int));
BinaryExpression numLessThanFive = Expression.LessThan(numParam, five);
Expression<Func<int, bool>> lambda1 =
    Expression.Lambda<Func<int, bool>>(
        numLessThanFive,
        new ParameterExpression[] { numParam });

Beyond this, nothing else happens. So this is the object graph that might then be passed into your method.



回答2:

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 the Expression<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:

public static string GetName<T>(this T item) where T : class
{
    if (item == null)
        return string.Empty;

    return typeof(T).GetProperties()[0].Name;
}

You can use it to get name of property or a variable, like this:

new { property }.GetName();

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 reflection

A related question: Get all the property names and corresponding values into a dictionary