\"nameof\" expression is introduced in Visual Studio 2015 and c# 6
nameof (C# and Visual Basic Reference)
How can u use it or write a similar method in older
To my knowledge there are three options to not have to use a magic string
nameof which requires Visual Studio 2015 (But can be compiled to other versions of the .net framework)
nameof(this.Property)
use a method that takes an expression and returns the property name as found in this post "Get string name of property using reflection"
var propertyName = GetPropertyName(
() => myObject.AProperty); // returns "AProperty"
CallerMemberNameAttribute - (Only available in .net framework 4.5, included because original post said older versions like .net framework 4.0 which I guess includes 4.5) The draw back of this method is it is only useful when you need to string representation of the current method you are operating in.
public string IsChecked {
set{
Console.WriteLine(GetCurrentMemberName()); // prints "IsChecked"
}
}
string GetCurrentMemberName([CallerMemberName] string memberName = "")
{
return memberName;
}