使用语句和微软的源代码分析嵌套

使用语句和微软的源代码分析嵌套

本文介绍了使用语句和微软的源代码分析嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我上额外的代码转换分析规则。
出乎我的意料,我看到违规的地方,我总是考虑的最佳实践。
。如果我有两个嵌套的一次性用品,我把两个using语句是这样的:

Recently I switched on additional code analyses rules.To my surprise I saw a violation in a place I was always considering as the best practice.If I have two nested disposables I am putting two using statements like this:

    using (StringReader strReader = new StringReader(xmlString))
    using (XmlReader xmlReader = XmlReader.Create(strReader))
    {
        result.ReadXml(xmlReader);
    }

这也对应于高额定Q&安培; A
语句

This also corresponds to the high rated Q&ANested using statements in C#

侵犯我得到以下状态:

Warning 18  CA2202 : Microsoft.Usage : Object 'strReader' can be disposed more
than once in method '????'. To avoid generating a System.ObjectDisposedException
you should not call Dispose more than one time on an object.: Lines: ??



我所做的是一个直观的尝试和错误,思考外流的那么近也可能会处理的是什么内层我快速固定这样我的代码:

What I did was an intuitive try and error, thinking that close of outer stream will also probably dispose the inner one I quick fixed my code like this:

    using (XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString)))
    {
        result.ReadXml(xmlReader);
    }



胡拉!警告已经一去不复返了。但是,田田!新一发生:

Hura! The warning is gone. But, tada! The new one occurred:

Warning 18  CA2000 : Microsoft.Reliability : In method '????????', object
'new StringReader(xmlString)' is not disposed along all exception paths. Call
System.IDisposable.Dispose on object 'new StringReader(xmlString)' before all
references to it are out of scope.



然后我发现了一个很丑陋的解决方案:

Then I found a very ugly solution:

    {
        StringReader strReader = null;
        try
        {
            strReader = new StringReader(xmlString);
            using (XmlReader xmlReader = XmlReader.Create(strReader))
            {
                strReader = null;
                result.ReadXml(xmlReader);
            }
        }
        finally
        {
            if (strReader != null) strReader.Dispose();
        }
    }



作为最后一步(就像每一个优秀的程序员)我看着帮助页面CA2202和我的惊讶正是我最后一次丑陋的解决方案被提出来解决这个问题?

As a very last step (like every good programmer) I looked into help page for CA2202 and to my surprise exactly my last UGLY solution was proposed to fix the issue?

有尝试{}最后用杂波周围的代码非常多!对于被嵌套使用我更可读

Having try{} finally around using clutters the code very much! For me is the nested using much more readable.

问:是否有做事情的更好的办法?我在找这将是直观易懂的解决方案。大家谁看到这最后的片段将要发生的事情珍玩。

Question: Is there a better way of doing things? I am looking for a solution which will be intuitively understandable. Everyone who will see this last snippet will be curios about what is happening.

在提前为您解答感谢。

推荐答案

现在的问题是,因为嵌套usings不是。他们都很好,一般建议使用。这里的问题是,的XmlReader 将处置的TextReader 如果你传递一个 XmlReaderSettings CloseInput ==真,但CA2202规则不是足够聪明,你的代码将不会走这分支。保持你的嵌套usings,并抑制CA2202违规为误报。

The problem isn't because of the nested usings. They're fine and generally recommended. The problem here is that XmlReader will dispose the TextReader if you pass an XmlReaderSettings with CloseInput == true, but the CA2202 rule isn't smart enough that your code won't go down that branch. Keep your nested usings, and suppress the CA2202 violation as a false positive.

如果您想明确在你的代码,以增强其可读性和/或维护,使用 XmlReaderSettings CloseInput 设置为,但是这是默认值,所以它不是绝对必要的,而且,是明确,将无法满足该规则。

If you want to be explicit in your code in order to enhance its readability and/or maintainability, use an XmlReaderSettings with CloseInput set to false, but that is the default value, so it's not strictly necessary, and, to be clear, would not satisfy the rule.

BTW,也有类似的问题CA2202为场景的多种流和读卡器类型。不幸的是,他们是各不相同的,因为这一个,所以最好的情况下处理可根据其类型是导致该问题有所不同。

BTW, there are similar CA2202 problem scenarios for a variety of stream and reader types. Unfortunately, they're not all the same as this one, so the best case handling can differ depending on which type is cause the problem.

这篇关于使用语句和微软的源代码分析嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:33