Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "nameof" keyword with set-only property

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).

like image 463
Sam Avatar asked Mar 04 '16 10:03

Sam


1 Answers

It turned out a ReSharper bug, at least the error is not showing when ReSharper is suspended (Tools -> Options -> ReSharper Ultimate -> Suspend Now), and project successfully compiles even with errors displayed in Visual Studio.

My current versions are:

  • JetBrains ReSharper Ultimate 10.0.2 Build 104.0.20151218.120627 (ReSharper 10.0.20151218.130009)
  • Visual Studio 14.0.24720.0

Update

While I was searching how to submit bugs to the ReSharper team, I found a very similar issue in their tracker. It is about class member property, but it gives me the same error as with my interface, so I believe this is the same bug. There said it will be fixed in ReSharper 10.1.

like image 169
Sam Avatar answered Sep 28 '22 05:09

Sam