问题
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