简体   繁体   English

为什么ReSharper在动态类型上建议可能的NullReferenceException?

[英]Why is ReSharper suggesting a possible NullReferenceException on a dynamic type?

If I write the following code, ReSharper is warning me for a possible NullReferenceException . 如果我编写以下代码,ReSharper会警告我可能存在NullReferenceException However I'm explicitly checking for null in the statement above. 但是我在上面的语句中明确检查null Is there something about dynamic I do not know about (is it assuming it might be backed by a IEnumerable or something like that)? 有什么关于dynamic我不知道(是假设它可能由IEnumerable或类似的东西支持)? Or is this a glitch with ReSharper? 或者这是ReSharper的故障? Or something else? 或者是其他东西?

dynamic user = connection.Query("SELECT ...").FirstOrDefault(); // Dapper Extension
if (user == null)
    return null;

return new User(user.username);
//              ^^^^
// (local variable) dynamic user
//
// Possible 'System.NullReferenceException'

The issue is that user == null is a dynamic call; 问题是user == null是一个动态调用; R# can't assume that the runtime type of the user object will have an equality operator that works properly. R#不能假设user对象的运行时类型将具有正常工作的相等运算符。 It could very easily have: 它很容易有:

public static bool operator ==(Foo x, Foo y) { return false; }
public static bool operator !=(Foo x, Foo y) { return true; }

in which case, user == null would always return false , even if the user variable was a null reference. 在这种情况下,即使user变量是null引用, user == null也总是返回false

Try changing the code to: 尝试将代码更改为:

if (ReferenceEquals(user, null)) return null;
return new User(user.username);

NB: The problem only appears when you have the "Assume entity value can be null" option set to "When entity doesn't have explicit NotNull attribute". 注意:仅当“假定实体值可以为空”选项设置为“当实体没有显式的NotNull属性”时,才会出现此问题。

Try this : 试试这个 :

dynamic user = connection.Query("SELECT ...").FirstOrDefault(); // Dapper Extension
if (user != null)
    return new User(user.username);

return null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Resharper 告诉我可能存在 NullReferenceException? - Why does Resharper tell me there is a possible NullReferenceException? Resharper建议只读字段,为什么? - Resharper suggesting readonly field, why? ReSharper:Enumerator可能出现NullReferenceException? - ReSharper: Possible NullReferenceException with Enumerator? ReSharper“可能的NullReferenceException”错误的FileInfo? - ReSharper “Possible NullReferenceException” wrong with FileInfo? 可能的NullReferenceException ReSharper代码分析C# - Possible NullReferenceException ReSharper code analysis C# Resharper建议字符串文字是可本地化的 - Resharper is suggesting that string literals are localizable ReSharper:如何删除“可能的‘System.NullReferenceException’”警告 - ReSharper: how to remove "Possible 'System.NullReferenceException'" warning 可能在Resharper中有不正确的“可能的'System.NullReferenceException'”提示 - Maybe incorrect “Possible 'System.NullReferenceException'” tip in Resharper 尽管存在隐式null检查,但可能存在System.NullReferenceException的ReSharper警告 - ReSharper warning for Possible System.NullReferenceException despite implicit null check Resharper false可能带有任务并行库的nullReferenceException警告 - Resharper false possible nullReferenceException warning with task parallel library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM