Search code examples
c#propertiesnameof

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


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

Solution

  • 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.