Using the keyword "nameof" using the set-only property

In my function, I get objects that implement the IMediaPanel interface:

 public interface IMediaPanel { bool IsListsAreaVisible { get; } bool IsNextEntityExists { set; } } private void InitConnections(IMediaPanel panelControl) { // Initialization logic } 

During initialization, I need to specify the property names for which I use the 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 recipient.

The search for "name restrictions" did not give me an answer to this question; moreover, official comments do not contain restrictions on getters properties:

... The following errors should be noted: predefined types (for example, int or void), types with a null value (Point?), Array types (Customer [,]), pointer types (Buffer *), qualified alias (A :: B ) and unrelated common types (Dictionary <,>), preprocessing characters (DEBUG), and labels (loop:) ....

Can someone explain why this restriction exists, and if there is any link to it? What reason can force the nameof keyword to use an attribute of a property instance while it should (as I assume) just use generic type information through Reflection? (at least in this particular case, when I cannot directly specify an instance property due to an unknown type, I just know that this instance implements the interface)

Update

To explain why the @Gusdor suggestion from the comments does not work, I need to clarify what I call the InitConnections function (in simplified form):

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

So, if I use nameof(panelControl.IsNextEntityExists) inside the Init function, this will nameof(panelControl.IsNextEntityExists) an error because FrameworkElement does not contain the custom client property IsNextEntityExists . And if I use the same expression inside the InitConnections function, I get an error message - the same as with nameof(IMediaPanel.IsNextEntityExists) .

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

+5
source share
1 answer

It turned out that the ReSharper error, at least the error does not appear when pausing ReSharper (Tools → Options → ReSharper Ultimate → Suspend Now), and the 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 looking for how to send errors to the ReSharper team, I found a very similar issue in my tracker. This is a class property, but it gives me the same error as in my interface, so I think this is the same error. It says that it will be fixed in ReSharper 10.1.

+5
source

Source: https://habr.com/ru/post/1244419/


All Articles