如何nameof工作? [英] How does nameof work?

查看:118
本文介绍了如何nameof工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是怎么来nameof从C#6,可以访问非静态属性一样,如果它是静态的。下面是一个例子。



 公共类识别TestClass 
{
公共字符串名称{;组; }
}

公共类测试
{
公开测试()
{
字符串名称= nameof(TestClass.Name); //什么让speciall约nameof
//字符串名称2 = TestClass.Name;这不会很明显编译,
}
}


解决方案

这不是访问属性 - 即运营商纯粹是一个编译器机制注入了名的说法成代码。在这种情况下,它将会取代 nameof(TestClass.Name)名称。它的非静态的事实是无关的。


I was just wondering how come nameof from C# 6, can access non static property just like if it was static. Here is an example

public class TestClass
{
    public string Name { get; set; }
}

public class Test
{
    public Test()
    {
        string name = nameof(TestClass.Name); // whats so speciall about nameof
        //string name2 = TestClass.Name; this won't compile obviously, 
    }
}

解决方案

It's not "accessing" the property - that operator is purely a compiler mechanism to inject the "name" of the argument into the code. In this case it will replace nameof(TestClass.Name) with "Name". The fact that it's non-static is irrelevant.

这篇关于如何nameof工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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