C# 6: nameof() current property in getter/setter
Asked Answered
M

2

13

Is there a way to get the name of the current property in a getter/setter?

Something like this:

public string MyProperty
{
    get { return base.Get<string>(nameof(ThisProperty)); }
    set { base.Set<string>(nameof(ThisProperty), value); }
}

nameof(ThisProperty) should resolve to "MyProperty".

Merrile answered 31/3, 2016 at 15:39 Comment(5)
nameof(MyProperty) should work just fine?Cassell
Why should there? What is the problem with writing nameof(MyProperty)?Certes
This could be useful in a case where the name MyProperty might change: then, you have to remember to update nameof(whatever), although the Visual Studio IDE should catch this problem.Diannadianne
@Diannadianne yes, the IDE should catch this as this was (afaik) one of the main reasons for introducing nameof.Certes
I came across this question when using MVVM's RaisePropertyChanged(nameof(ThisProperty)) method and trying to avoid cut and paste issues when generating new properties. I then looked more closely at MVVMLight's method signature and saw they already have an overload for RaisePropertyChanged() defined as public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null); exactly for this purpose :-)Deuterium
F
26

It can't be done with nameof, but there's an even better way (available since C# 5). You can make the propertyName parameter optional and apply the CallerMemberName attribute to it:

protected void Set<T>(T value, [CallerMemberName] string propertyName = null)
{
    ...
}

protected T Get<T>([CallerMemberName] string propertyName = null)
{
    ...
}

Now if you omit the argument for propertyName, the current member name is passed implicitly:

public string MyProperty
{
    get { return base.Get<string>(); } // same as calling Get<string>("MyProperty")
    set { base.Set<string>(value); } // same as calling Set<string>(value, "MyProperty")
}
Faultfinder answered 31/3, 2016 at 15:45 Comment(4)
I beg to differ with your first sentence. As of C# 6, nameof will actually do the job: public string MyProperty { get { return base.Get<string>(nameof(MyProperty)); } set { base.Set<string>(nameof(MyProperty), value); } }Blandishments
@takrl, I think the OP wanted to get the name of the property without having to specify it explicitly. If you use nameof(MyProperty), you still have to write MyProperty yourself (admittedly, it's still better than writing it as a string)Faultfinder
I agree that the use of [CallerMemberName] is probably better in this context, but still, the first sentence seems to suggest nameof doesn't work for properties, which is not true.Blandishments
Is it not suppose to be : "[CallerMemberName] string propertyName = null" ?Nonetheless
H
-3

Alternative is to the the MethodBase since a Get and Set are essentially methods.

public string MyProperty
{
    get
    {
        return MethodBase.GetCurrentMethod().Name.Substring(4);
    }            
}

The substring is there because each name is prefixed with get_ and set_

This returns MyProperty as the result.

Helbonna answered 31/3, 2016 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.