为什么 nameof() 在 Linq 表达式中给出了不明确的调用警告,但当我使用与字符串相同的值时却没有?
Why does nameof() give an ambiguous invocation warning in a Linq expression, but not when I use the same value as a string?
当我升级到 FluentAssertions 4.2.2 时,我收到编译器警告。在下面的代码中,如果我调用 EndsWith(nameof(x))
,我会收到一个不明确的调用警告。相反,如果我定义 var foo = nameof(x)
并调用 EndsWith(foo)
,它会干净地编译。代码在两种情况下都运行正常。
我的问题是为什么会发生这种情况,除了将 nameof()
结果存储在变量中之外,是否有其他解决方法?
[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;
}
}
您确定这是编译器 error/warning 而不是 ReSharper 警告吗?
如果是前者,CSNNNNerror/warning号是多少?
看过(Resharper: Ambiguous Invocation)
当我升级到 FluentAssertions 4.2.2 时,我收到编译器警告。在下面的代码中,如果我调用 EndsWith(nameof(x))
,我会收到一个不明确的调用警告。相反,如果我定义 var foo = nameof(x)
并调用 EndsWith(foo)
,它会干净地编译。代码在两种情况下都运行正常。
我的问题是为什么会发生这种情况,除了将 nameof()
结果存储在变量中之外,是否有其他解决方法?
[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;
}
}
您确定这是编译器 error/warning 而不是 ReSharper 警告吗?
如果是前者,CSNNNNerror/warning号是多少?
看过(Resharper: Ambiguous Invocation)