从LINQ表达式中检索信息时使用反射? [英] Is reflection used when retrieving information from a linq expression?

查看:126
本文介绍了从LINQ表达式中检索信息时使用反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的扩展方法:

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;
}



我使用LINQ表达式传递属性名称时,而不是字符串提供强有力的打字我用这个功能来检索属性作为字符串的名称。请问这种方法使用反射?

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.

推荐答案

据我所知,反射不参与这个意义上讲,某种动态类型的内省发生在幕后。不过,从的System.Reflection 键入的PropertyInfo 与来自 System.Linq.Expressions 命名空间类型一起使用。他们所使用的编译器只描述任何 Func键< T,E> 传递给你的方法是一个抽象语法树(AST)。由于从 Func键和下这种转变,T,E> 来表达式树是由编译器完成的,而不是在运行时,只有拉姆达的静态方面进行描述

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.

但请记住该建筑物从在运行时的lambda这个表达式树(复杂的对象图)可能需要稍长不是简单地传递一个属性名字符串(单个对象),只需因为越来越多的对象需要被实例化(的数量取决于传递给你的方法拉姆达的复杂性),但同样,没有动态型式检验单 someObject.GetType() 。参与

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.

示例:

这个MSDN文章显示以下lambda表达式:

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.

这篇关于从LINQ表达式中检索信息时使用反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆