在没有对象实例化的情况下,在编译时获取类的属性名称 [英] Get class's property name at compile time without object instantiation

查看:111
本文介绍了在没有对象实例化的情况下,在编译时获取类的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在编译时获得类属性的名称(注意!),而无需实例化对象?
通过实例化,可以使用nameof()轻松完成:

Is it possible to get the name of class's property (attention!) at compile time and without object instantiation?
With instantiation it can easely be done with nameof():

class DummyClass
{
    public int DummyProperty { get; set; }
}
void Meth()
{
    //With instantiation
    var dc = new DummyClass();
    var prname = nameof(dc.DummyProperty);
}

推荐答案

如果我对您的理解正确,则可以使用nameof(DummyClass.DummyProperty).

You may use nameof(DummyClass.DummyProperty), if I understood you correctly.

用于获取变量,类型或成员的简单(非限定)字符串名称.

Used to obtain the simple (unqualified) string name of a variable, type, or member.

这篇关于在没有对象实例化的情况下,在编译时获取类的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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