4

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;
        ...
  • possible duplicate of [How to get current property name via reflection?](http://stackoverflow.com/questions/1206023/how-to-get-current-property-name-via-reflection) – empi May 02 '12 at 09:25
  • There are a lot of methods to achieve this. The best one depends on the reason you need it. To implement INotifyPropertyChanged? – Adriano Repetti May 02 '12 at 09:37
  • @Adriano: I updated with my reason for asking. –  May 02 '12 at 09:43
  • See the post of @empi but remember to add MethodImplOptions.NoInlining. If you're using .NET 4.5 (or you know you'll use it) take a look to CallerMemberNameAttribute (and it's even resolved at compile time!). – Adriano Repetti May 02 '12 at 09:49
  • check this link [Get string name of property using reflection][1] [1]: http://stackoverflow.com/questions/3661824/get-string-name-of-property-using-reflection – Rooney80 May 02 '12 at 09:56

1 Answers1

2

You could use reflection:

[MethodImpl(MethodImplOptions.NoInlining)]
set
{
    string name = MethodBase.GetCurrentMethod().Name;
    // TODO: strip the set_ prefix from the name
}

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 equal set_Bar.

So:

string name = MethodBase.GetCurrentMethod().Name.Substring(4);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The method will be (or could be) inlined. He should mark it as non inlined. – Adriano Repetti May 02 '12 at 09:30
  • 3
    @sixfeetsix if the property setter is simple enough (as it should almost always be) it'll be inlined by the JIT compiler. In this case the name of the current method is not set_propertyName but the name of the caller. It should mark a method as [MethodImpl(MethodImplOptions.NoInlining)] – Adriano Repetti May 02 '12 at 09:36
  • @Adriano, excellent remark. Thanks for pointing that out. I have updated my answer to take it into account. – Darin Dimitrov May 02 '12 at 09:45
  • +1 This is the fastest method to get the property name in .NET 3.x and 4.0! – Adriano Repetti May 02 '12 at 10:13
  • @Adriano: is it the fastest when you factor in the hit from disallowing inlining? –  May 02 '12 at 10:45
  • @sixfeetsix fastest compared with StackFrame and Expressions! The Noinline is the price we have to pay... :( – Adriano Repetti May 02 '12 at 10:49