4

I'm getting a compiler warning that started happening when I upgraded to FluentAssertions 4.2.2. In the following code, if I call EndsWith(nameof(x)), I get an ambiguous invocation warning. If instead I define var foo = nameof(x) and call EndsWith(foo), it compiles cleanly. The code runs ok in both scenarios.

My questions are why is this happening, and is there a workaround other than storing the nameof() result in a variable?

[Test]
public void TestLastNamesAreSame()
{
    var original = new MyDTO("fred", "jones");
    var expected = new MyDTO("barney", "jones");

    // this gives an Ambiguous invocation warning
    expected.ShouldBeEquivalentTo(original, o => o
        .Excluding(x => x.SelectedMemberPath.EndsWith(nameof(MyDTO.FirstName))));

    // but when I use a variable holding the same value, it works without warning
    const string nameOfFirstNameField = nameof(MyDTO.FirstName);
    expected.ShouldBeEquivalentTo(original, o => o
        .Excluding(x => x.SelectedMemberPath.EndsWith(nameOfFirstNameField)));
}

public class MyDTO
{
    public string FirstName { get; }
    public string LastName { get; }

    public MyDTO(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}
herky
  • 43
  • 5
  • 1
    Can you show your `MyDTO` class? I created a simple class with `LastName` and `FirstName` properties and a constructor, but I couldn't reproduce it. Both worked. – Szabolcs Dézsi Feb 17 '16 at 22:15
  • @SzabolcsDézsi this turns out to be a (spurious?) Resharper warning, not a compiler warning, so I'm not quite as concerned although it's still annoying. I've edited the question to include the MyDTO class, although you'll likely only see the warning if you're using R#. – herky Feb 17 '16 at 22:52
  • I am using ReSharper 10 and I don't see a warning. – Szabolcs Dézsi Feb 17 '16 at 22:52
  • Although I can see some issues in ReSharper's issue tracker related to `nameof` and false warnings. So probably it was already fixed. – Szabolcs Dézsi Feb 17 '16 at 22:57
  • Thanks for that update @SzabolcsDézsi. I have Resharper 9.2, so that's probably the issue. – herky Feb 17 '16 at 23:11

1 Answers1

2

Are you sure that this is a compiler error/warning and not a ReSharper warning?

If it is the former, what is the CSNNNN error/warning number?

Have look at (Resharper: Ambiguous Invocation)

Community
  • 1
  • 1
Jens Meinecke
  • 2,904
  • 17
  • 20
  • I think you're probably right. I'll verify and respond here. Regardless, thanks for the tip. – herky Feb 17 '16 at 22:31
  • As Übercoder suggested, this is a Resharper issue that Szabolcs Dézsi has verified is fixed in Resharper 10. – herky Feb 17 '16 at 23:17