5

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

7
  • Doesnameof(panelControl.IsNextEntityExists) work?
    – Gusdor
    Mar 4, 2016 at 10:07
  • 6
    I can't reproduce this - your exact code works fine for me. Can you post a minimal reproducible example?
    – Jon Skeet
    Mar 4, 2016 at 10:10
  • @Gusdor No, because I receive panelControl as FrameworkElement. Physically it is just custom UserControl, created by client which using my common control. Each client creates it's own user control and pass it to me, so we communicate through interface.
    – Sam
    Mar 4, 2016 at 10:18
  • I'm confused, your init method takes an IMediaPanel and you state that you need the names during init - "During initialization I need to specify properties names"
    – Gusdor
    Mar 4, 2016 at 10:21
  • 2
    @Sam: I wasn't even in Visual Studio - just compiling from the command line :)
    – Jon Skeet
    Mar 4, 2016 at 10:43

1 Answer 1

5

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.