I would like to get property name when I\'m in it via reflection mechanism. Is it possible?
Update: I have code like this:
public CarType Car
Slightly confusing example you presented, unless I just don't get it.
From C# 6.0 you can use the nameof operator.
public CarType MyProperty
{
get { return (CarType)this[nameof(MyProperty)]};
set { this[nameof(MyProperty)] = value]};
}
If you have a method that handles your getter/setter anyway, you can use the C# 4.5 CallerMemberName attribute, in this case you don't even need to repeat the name.
public CarType MyProperty
{
get { return Get(); }
set { Set(value); }
}
public T Get([CallerMemberName]string name = null)
{
return (T)this[name];
}
public void Set(T value, [CallerMemberName]string name = null)
{
this[name] = value;
}