我应该如何处理String.Format()产生一个“未经证实”的结果? [英] How should I handle String.Format() producing a result that is "unproven"?

查看:62
本文介绍了我应该如何处理String.Format()产生一个“未经证实”的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中,return语句被标记为"Code Contracts:requires unproven:!String.IsNullOrEmpty(message)"。


I我明确地使用String.Format将消息分配给明显不为空的字符串。  消除此警告的最佳方法是什么?


 


感谢您的帮助。


 

                      message  =   String 格式 @"可能 病毒, 文件 无法修复:  {0}" ,  origFileName); 
                     日志错误(消息);
                     return   new   ValidationFault (消息);

 

解决方案

Daniel,


是的,通常情况是某些外部方法的后置条件太弱(在本例中为String.Format)。要解决这个问题,您可以使用以下方法:


1。使用Contract.Assume(!String.IsNullOrEmpty(message));在致电新验证之前。


   这指示静态检查器"仅仅相信属性"。在运行时,它充当运行时检查。


2。在此方法上使用[SuppressMessage(...)]来抑制此特定警告。要查看要使用的确切警告掩码,请将以下选项添加到属性窗格的额外静态检查器选项中:  -outputwarningmasks。然后在输出
窗口中,找到与此特定警告对应的警告掩码,并将其添加到suppress消息属性中。注意:只有在不知情的情况下不能使用选项1时才应该这样做。并且只有在您的代码没有被持续修改时才会这样做,因为如果您的代码发生变化,警告
掩码会发生变化。


3。在ValidationFault构造函数上使用[SuppressMessage(...)]来抑制与此警告关联的所有静态警告。与2下方相同,但使用屏蔽所有来电者警告("需要通话......")


 


In this snippet of code, the return statement is marked to say that "Code Contracts: requires unproven: !String.IsNullOrEmpty(message)".

I am clearly assigning message to a string that is clearly not empty, using String.Format.   What is the best way to eliminate this warning?

 

Thanks for your help.

 

                    message = String.Format(@"Possible virus, file can't be repaired: {0}", origFileName);
                    Log.Error(message);
                    return new ValidationFault(message);

 

解决方案

Daniel,

yes, it is often the case that some external method has too weak a postcondition (in this case String.Format). To get around this, you can use the following approaches:

1. use Contract.Assume( !String.IsNullOrEmpty(message) ); before the call to new Validation.

    This instructs the static checker to "just believe the property". At runtime, it acts as a runtime check.

2. Use [SuppressMessage(...)] on this method to suppress this particular warning. In order to see what exact warning mask to use, add the following option to the property pane's extra static checker options: -outputwarningmasks. Then in the output window, find the warning mask corresponding to this particular warning and add that to your suppress message attribute. NOTE: you should only do this if you somehow cannot use option 1. and only if your code is not being modified continually, since the warning masks change if your code changes.

3. Use [SuppressMessage(...)] on the ValidationFault constructor to suppress ALL static warnings associated with this warning. Do the same as under 2. but use the mask that suppresses all caller warnings ("Requires-At-Call ...")

 


这篇关于我应该如何处理String.Format()产生一个“未经证实”的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆