Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ReSharper:如何删除;可能';System.NullReferenceException'&引用;警告_C#_.net_Configuration_Resharper_Nullreferenceexception - Fatal编程技术网

C# ReSharper:如何删除;可能';System.NullReferenceException'&引用;警告

C# ReSharper:如何删除;可能';System.NullReferenceException'&引用;警告,c#,.net,configuration,resharper,nullreferenceexception,C#,.net,Configuration,Resharper,Nullreferenceexception,下面是一段代码: IUser user = managerUser.GetUserById(UserId); if ( user==null ) throw new Exception(...); Quote quote = new Quote(user.FullName, user.Email); 这里一切都很好。但如果我将“if”行替换为以下行: ComponentException<MyUserManagerException>.FailIfTrue(user =

下面是一段代码:

IUser user = managerUser.GetUserById(UserId);
if ( user==null ) 
    throw new Exception(...);

Quote quote = new Quote(user.FullName, user.Email);
这里一切都很好。但如果我将“if”行替换为以下行:

ComponentException<MyUserManagerException>.FailIfTrue(user == null, "Can't find user with Id=" + UserId);
ComponentException.FailIfTrue(user==null,“找不到Id=“+UserId”的用户);
其中,功能实现如下所示:

public abstract class ComponentException<T> : ComponentException
        where T : ComponentException, new()
{
    public static void FailIfTrue(bool expression, string message)
    {
        if (expression)
        {
            T t = new T();
            t.SetErrorMessage(message);
            throw t;
        }
    }
    //...
}
公共抽象类ComponentException:ComponentException
其中T:ComponentException,new()
{
公共静态void FailIfTrue(布尔表达式、字符串消息)
{
if(表达式)
{
T=新的T();
t、 SetErrorMessage(消息);
掷t;
}
}
//...
}
然后ReSharper生成一条警告:可能是“System.NullReferenceException”指向第一次使用“user”对象

问题1。为什么会产生这样的异常?据我所知,如果
user==null
,那么将生成异常,执行将永远不会到达使用点

问题2。如何删除该警告?请注意: 1.我不想用注释来抑制这个警告(我会有很多类似的片段,不想在“注释垃圾”中转换我的源代码); 2.我不想更改ReSharper设置以将此问题从警告更改为“提示”的“建议”

谢谢

欢迎有任何想法


另外,我使用的是resharper 5.1,MVSV 2008,C#

Q1:因为resharper不做路径分析。它只看到一个可能的
null
引用并标记该引用


问题2:如果不执行您已经提供的任何一项,您都无法执行。这是由Resharper引擎造成的。发生这些“可能的NullReferenceException”是因为有人(可能在Resharper)在某个地方声明/配置了方法上的注释

以下是它的工作原理:

不幸的是,有时这些有用的注释是错误的

当您检测到错误时,应该将其报告给JetBrains,他们将在下一版本中更新注释。他们已经习惯了


同时,你可以试着自己修复它。阅读本文了解更多信息:)

Resharper只查看当前方法进行分析,而不递归分析您调用的其他方法

不过,您可以稍微引导Resharper,并为其提供有关某些方法的元信息。例如,它知道“Assert.IsNotNull(a)”,并将在分析时考虑这些信息。可以为Resharper创建一个外部注释文件,并为其提供有关某个库的额外信息,以便更好地进行分析。也许这可以提供一种解决你问题的方法

可以找到更多信息

可以找到一个示例,显示它如何用于库Microsoft.Contracts。

您知道(或期望)如果存在空引用,此代码将引发异常:

ComponentException<MyUserManagerException>.FailIfTrue([...]);

这将消除警告,并作为额外的奖励,在调试模式下进行运行时检查,以确保调用
FailIfTrue
后假设的条件确实满足。

旧帖子中的新答案

下面是关于如何通过ContractAnnotation与Resharper一起使用CodeContract的代码示例:

    [ContractAnnotation("value:null=>true")]
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }
这很简单…如果你在树林里找到面包屑。你也可以检查其他情况


祝您有愉快的一天

请检查您是否有任何用户==null,如果在给定代码上方进行检查。如果有,那么ReSharper认为变量“可以为null”,因此建议您在引用它之前使用check/assert。在某些情况下,这是ReSharper猜测变量是否可以为null的唯一方法。

我希望这是真的。。。Resharper 5.1无法正确识别
Debug.Assert(user!=null)
并引发警告。有什么配置我需要做吗?呃,R#5.1已经过时了,所以我没有什么可以测试你的问题。但是,有一些提示:确保您的代码正确引用了
System.Diagnostics.Debug
类,因为代码契约是为此定义的,而不是针对具有该名称/签名的任意方法。另外,请确保将生成配置设置为“调试”(否则代码超出范围,因此会弹出警告!)。如果您知道某个方法总是引发异常,则可以执行
[ContractAnnotation(=>halt”)]
    [ContractAnnotation("value:null=>true")]
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }