CallerMemberNameAttribute是否使用反射 [英] Does CallerMemberNameAttribute use reflection

查看:116
本文介绍了CallerMemberNameAttribute是否使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用 CallerMemberName 属性来避免指定成员名称作为实现INotifyPropertyChanged接口时被调用方法的String参数.

You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method when implementing INotifyPropertyChanged interface.

问题是它在幕后使用反射吗?硬编码属性名称会影响性能吗?

The question is does it use reflection behind the scene? Are there any performance hit over hard coding Property name?

推荐答案

否;编译器直接在编译过程中对成员名称进行硬编码.就IL而言,这是ldstr.例如,如果我们编译:

No; the compiler hard-codes the member-name directly during compilation. In terms of the IL, this is ldstr. For example if we compile:

static void Implicit()
{
    Log();
}
static void Explicit()
{
    Log("Explicit");
}
static void Log([CallerMemberNameAttribute] string name = null)
{}

我们得到:

.method private hidebysig static void Implicit() cil managed
{
    .maxstack 8
    L_0000: ldstr "Implicit"
    L_0005: call void Program::Log(string)
    L_000a: ret 
}
.method private hidebysig static void Explicit() cil managed
{
    .maxstack 8
    L_0000: ldstr "Explicit"
    L_0005: call void Program::Log(string)
    L_000a: ret 
}

您可以看到-IL具有直接 完全 烘焙的名称,就像我们手动输入一个字符串一样.

As you can see - the IL has the name baked in directly exactly the same as if we put a string in manually.

这篇关于CallerMemberNameAttribute是否使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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