Is it possible to get a unique identifier of a property from within its accessors?
class Foo
{
int Bar
{
set
{
string nameOfThisProperty = X; // where X == "Bar" or any unique value
}
}
}
If so, how?
Update:
The reason I’m asking is: I want some some consistent unique value identifying the property in which the code is executing to avoid having to declare one myself as I’m doing right now:
Dictionary<string, RelayCommand> _relayCommands
= new Dictionary<string, RelayCommand>();
public ICommand SomeCmd
{
get
{
string commandName = "SomeCmd";
RelayCommand command;
if (_relayCommands.TryGetValue(commandName, out command))
return command;
...
You could use reflection:
As pointed out in the comments section the setter could be inlined so it must be decorated with the
[MethodImpl]attribute to prevent the JITer from doing so.Also you will have to strip the
set_prefix from the method name because name will equalset_Bar.So: