Using "nameof" keyword with set-only property

Viewed 269

In my function I receive objects implementing IMediaPanel interface:

public interface IMediaPanel
{
    bool IsListsAreaVisible { get; }
    bool IsNextEntityExists { set; }
}

private void InitConnections(IMediaPanel panelControl)
{
    // Initialization logic
}

During the initialization I need to specify properties' names, for which I'm using C# 6.0 nameof keyword:

nameof(IMediaPanel.IsListsAreaVisible)

This works fine, but with this expression:

nameof(IMediaPanel.IsNextEntityExists)

Visual Studio shows me the following error:

The property 'MyNamespace.IMediaPanel.IsNextEntityExists' has no getter.

Searching "nameof limitations" didn't give me any answer about this issue, moreover official remarks doesn't contain restriction about property getters:

... The following are worth mentioning that produce errors: predefined types (for example, int or void), nullable types (Point?), array types (Customer[,]), pointer types (Buffer*), qualified alias (A::B), and unbound generic types (Dictionary<,>), preprocessing symbols (DEBUG), and labels (loop:). ...

Can anyone explain why there is this restriction and if there is any reference about that? What reason can force nameof keyword to use property's instance getter while it should (as I guess) just use general type information through Reflection? (at least in this particular case, when I can't directly point to an instance's property due to unknown type, I just know that this instance implements the interface)

Update

To explain why @Gusdor's suggestion from comments is not working, I need to clarify how I call InitConnections function (in simplified form):

public void Init(FrameworkElement panelControl)
{
    // ... Other logic ...
    this.InitConnections((IMediaPanel) panelControl);
}

So if I use nameof(panelControl.IsNextEntityExists) inside Init function, it will produce an error because FrameworkElement doesn't contains custom client's IsNextEntityExists property. And if I use the same expression inside InitConnections function, I get an error about the getter - the same as with nameof(IMediaPanel.IsNextEntityExists).

Anyway, I found the answer, this 'getter' error is a ReSharper's bug (see my own answer).

1 Answers
Related