在没有对象实例化的情况下在编译时获取 class 的 属性 名称
Get class's property name at compile time without object instantiation
是否可以在编译时获取 class 的 属性 的名称(注意!)并且无需对象实例化?
通过实例化,可以使用 nameof() 轻松完成:
class DummyClass
{
public int DummyProperty { get; set; }
}
void Meth()
{
//With instantiation
var dc = new DummyClass();
var prname = nameof(dc.DummyProperty);
}
如果我理解正确,你可以使用 nameof(DummyClass.DummyProperty)
。
在 docs.
中有一个类似的用例示例
Used to obtain the simple (unqualified) string name of a variable, type, or member.
是否可以在编译时获取 class 的 属性 的名称(注意!)并且无需对象实例化?
通过实例化,可以使用 nameof() 轻松完成:
class DummyClass
{
public int DummyProperty { get; set; }
}
void Meth()
{
//With instantiation
var dc = new DummyClass();
var prname = nameof(dc.DummyProperty);
}
如果我理解正确,你可以使用 nameof(DummyClass.DummyProperty)
。
在 docs.
中有一个类似的用例示例Used to obtain the simple (unqualified) string name of a variable, type, or member.