Get string name of property using reflection

前端 未结 7 935
北海茫月
北海茫月 2020-11-28 06:56

There is a whole wealth of reflection examples out there that allow you to get either:

    1. All properties in a class

    2. A single property, provided you kn

7条回答
  •  没有蜡笔的小新
    2020-11-28 07:22

    With C# 6.0 (Visual Studio 2015), you can now use the nameof operator, like this:

    var obj = new MyObject();
    string propertyName = nameof(obj.Property);
    string methodName = nameof(obj.Method);
    string directPropertyName = nameof(MyObject.Property);
    string directMethodName = nameof(MyObject.Method);
    

提交回复
热议问题