Get class's property name at compile time without object instantiation

匆匆过客 提交于 2019-12-11 08:45:27

问题


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

回答1:


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

There is a similar example for such a use case at docs.

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



来源:https://stackoverflow.com/questions/50391110/get-classs-property-name-at-compile-time-without-object-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!