比如下面的 BooleanResult 中 bool 操作符重载
public class BooleanResult
{
public static implicit operator bool(BooleanResult booleanResult) => booleanResult.Success;
}
会触发 NullReferenceException,重现问题的测试代码
[Fact]
public void ImplicitConversionToBoolTest()
{
BooleanResult? result = null;
Assert.False(result == true);
}
修改为下面的实现解决了
public static implicit operator bool(BooleanResult? booleanResult) => booleanResult?.Success == true;