不处理意外的规则集 [英] Ruleset for not disposing conenctions

查看:82
本文介绍了不处理意外的规则集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我可以看到有一条规则(CA2000)可以确保处理应用程序,但是在我的例子中没有触发:

I can see that there is a rule(CA2000) for making sure that applications is disposed, but it does not trigger in my example:

using Adam.Core;
using System;
using System.IO.Ports;

namespace visual_studio_playground
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var app = new Application();
            app.LogOn("test", "test");
            SerialPort port = new SerialPort("COM4");
            port.Open();

        }
    }
}

在示例中我应该得到两个警告/错误,但没有显示。有没有办法将它们标记为不处理它们?

In the example I should be getting two warnings/errors, but none is displayed. Is there a way to mark these as when they are not disposed?

推荐答案

使用(SerialPort port =
new SerialPort " COM4" ))

Using (SerialPort port = new SerialPort("COM4"))

{

       其他代码。

        other code.

}

https://social.msdn.microsoft.com/Forums/vstudio/en-US/cdc4144b-c88e- 476f-bfb3-a2df103536d7 / ruleset-for-not-disposing-conenctions?forum = csharpgeneral

https://social.msdn.microsoft.com/Forums/vstudio/en-US/cdc4144b-c88e-476f-bfb3-a2df103536d7/ruleset-for-not-disposing-conenctions?forum=csharpgeneral

<复制>

<copied>

使用 语句调用
以正确的方式在对象上配置 方法,并且(当您使用时)它如前所示)它也会导致对象本身超出范围,因为 Dispose 被调用。在使用 块的
中,该对象是只读的,无法修改或重新分配。

The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

< end>



<end>


这篇关于不处理意外的规则集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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