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

1

2

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);
}
Chaldean answered 17/5, 2018 at 12:8 Comment(0)
E
4

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.

Excipient answered 17/5, 2018 at 12:18 Comment(3)
But it's not static?!Chaldean
@Chaldean It's still possibleExcipient
Indeed! Thanks!Chaldean

© 2022 - 2024 — McMap. All rights reserved.