它是确定做从内使用块一回 [英] Is it OK doing a return from inside using block

查看:129
本文介绍了它是确定做从内使用块一回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做代码审查,并已发现了很多的格式如下代码:

 公共MyResponse的MyMethod(串ARG)使用(示踪myTracer =新的示踪(Constants.TraceLog))
{
MyResponse ABC =新MyResponse
{
();

//一些代码

返回ABC;
}
}

当我运行代码分析,我收到了CA2000警告Microsoft.Reliability



如果代码被改写为:

 公开MyResponse的MyMethod(字符串ARG)
{
MyResponse ABC =新MyResponse();使用

(示踪myTracer =新的示踪(Constants.TraceLog))
{
//一些代码
}
返回ABC;
}



或者它不要紧?



修改



该生产线在其上报告警告:

  MyResponse ABC =新MyResponse(); 



MyResponse是一个标准的数据集。



完整的错误信息是:




警告150 CA2000:Microsoft.Reliability:在方法'XXXXX(GUID,GUID)',对象'MyResponse 是不是所有的沿路径异常处理。所有引用之前对象MyResponse呼叫System.IDisposable.Dispose它超出范围。



解决方案

您重写不会解决CA2000警告,因为这个问题是不是示踪对象,但 MyResponse 目标结果
规定的文件:




下面是一些情况下using语句是不足以保护IDisposable的对象,并可能导致CA2000发生。结果
返回的一次性对象需要构造对象在try / finally块using块之外。




要解决该警告的 不与的堆栈跟踪搞乱你的异常 (小于 - 点击,这是一个链接),使用此代码:

 公开MyResponse的MyMethod(字符串ARG)
{
MyResponse tmpResponse = NULL;
MyResponse响应= NULL;

{
tmpResponse =新MyResponse();使用

(示踪myTracer =新的示踪(Constants.TraceLog))
{
//一些代码
}

响应= tm​​pResponse ;
tmpResponse = NULL;
}
终于
{
如果(!tmpResponse = NULL)
tmpResponse .Dispose();
}
返回响应;
}



为什么呢?请参阅链接的文档中的例子。


I am doing a code review, and have found alot of code with the following format:

public MyResponse MyMethod(string arg)
{
    using (Tracer myTracer = new Tracer(Constants.TraceLog))
    {
        MyResponse abc = new MyResponse();

        // Some code

        return abc;
    }
}

When I run a code analysis I get a CA2000 warning Microsoft.Reliability

Should the code be rewritten as:

public MyResponse MyMethod(string arg)
{
   MyResponse abc = new MyResponse();

   using (Tracer myTracer = new Tracer(Constants.TraceLog))
   {
       // Some code
   }
   return abc;
}

Or does it not matter?

Edit

The line on which it is reporting the warning is:

MyResponse abc = new MyResponse();

MyResponse is a standard Dataset.

The full error message is:

Warning 150 CA2000 : Microsoft.Reliability : In method 'xxxxx(Guid, Guid)', object 'MyResponse ' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'MyResponse ' before all references to it are out of scope.

解决方案

Your rewrite will not fix that CA2000 warning, because the problem is not the Tracer object, but the MyResponse object.
The documentation states:

The following are some situations where the using statement is not enough to protect IDisposable objects and can cause CA2000 to occur.
Returning a disposable object requires that the object is constructed in a try/finally block outside a using block.

To fix the warning without messing with the stack trace of your exceptions (<- click, it's a link), use this code:

public MyResponse MyMethod(string arg)
{
   MyResponse tmpResponse = null;
   MyResponse response = null;
   try
   {
       tmpResponse = new MyResponse();

       using (Tracer myTracer = new Tracer(Constants.TraceLog))
       {
           // Some code
       }

       response = tmpResponse;
       tmpResponse = null;
    }
    finally
    {
        if(tmpResponse != null)
            tmpResponse .Dispose();
    }
    return response;
}

Why? Please see the example in the linked documentation.

这篇关于它是确定做从内使用块一回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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