Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可以在不使用反射的情况下从 dotnet 中的代码获取属性名称吗?
int PropWithNiceName { get {Console.Writeline( ??? )} }
是的; 您可以使用新[CallerMemberName]属性:
[CallerMemberName]
static string MemberName([CallerMemberName] string name = null) { return name; } int MyProperty { set { Console.WriteLine(MemberName()); } }