Why does nameof () give a warning of an ambiguous call in a Linq expression, but not when I use the same value as a string?

I get a compiler warning that started when upgrading to FluentAssertions 4.2.2. In the following code, if I call EndsWith(nameof(x)), I get an ambiguous call warning. If instead I define var foo = nameof(x)and invoke EndsWith(foo), it compiles cleanly. The code works fine in both scenarios.

My questions are, why is this happening, and is there a workaround other than storing the result nameof()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;
    }
}
+4
source share
1 answer

, / , ReSharper?

, / CSNNNN?

(Resharper: Ambiguous Invocation)

+2

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


All Articles