属性/方法内联和影响的反思 [英] Property / Method inlining and impact on Reflection

查看:112
本文介绍了属性/方法内联和影响的反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的答案就因此问题之一是由瓦伦丁Kuzub,谁认为,通过JIT编译器内联属性将导致反射停止工作评价。

My answer to one of the question on SO was commented by Valentin Kuzub, who argues that inlining a property by JIT compiler will cause the reflection to stop working.

案例如下:

class Foo
{
    public string Bar { get; set; }

    public void Fuzz<T>(Expression<Func<T>> lambda)
    {
    }
}

Fuzz(x => x.Bar);



Fuzz的函数接受一个lambda表达式和用途反射找物业。它是在的HtmlHelper 扩展MVC中的普遍做法。

Fuzz function accepts a lambda expression and uses reflection to find the property. It is a common practice in MVC in HtmlHelper extensions.

我不认为反射将停止工作即使酒吧属性,都会内联,因为它是酒吧将内联和呼叫 typeof运算(富).GetProperty(酒吧)仍然会返回一个有效的的PropertyInfo

I don't think that the reflection will stop working even if the Bar property gets inlined, as it is a call to Bar that will be inlined and typeof(Foo).GetProperty("Bar") will still return a valid PropertyInfo.

你能否确认这个,请还是我的方法内联的理解是错误的?

Could you confirm this please or my understanding of method inlining is wrong?

推荐答案

JIT编译器在运行时工作,并它不能改写存储在程序集元数据信息。和反射读取组件来访问元数据。所以没有从JIT编译器反射的影响。

JIT compiler operates at runtime and it can't rewrite metadata information stored in the assembly. And reflection reads assembly to access this metadata. So there are no impact from JIT-compiler to reflection.

编辑:
其实有几个地方时,C#编译器本身内联的编写过程中的一些信息。例如,常量,枚举和默认参数是内联,所以你不能反射时访问它们。但它绝对不涉及你的具体情况。

Actually there are couple of places when C# compiler itself "inlines" some information during compilation. For example, constants, enums and default arguments are "inlined" so you can't access them during reflection. But it definitely not related to your particular case.

这篇关于属性/方法内联和影响的反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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