是什么导致此代码中的"CA2202:请勿多次放置对象",我该如何重构? [英] What is causing 'CA2202: Do not dispose objects multiple times' in this code and how can I refactor?

查看:120
本文介绍了是什么导致此代码中的"CA2202:请勿多次放置对象",我该如何重构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有下面的函数,该函数用于在不添加XML声明的情况下序列化对象.我刚刚打开了一个包含Visual Studio 2012的项目,并且代码分析出现了"CA2202:请勿多次放置对象"警告.

I have the function below which is used to serialize an object without adding the XML declaration. I've just opened the project containing it an Visual Studio 2012 and the Code Analysis is coming up with the 'CA2202: Do not dispose objects multiple times' warning.

现在,在其他情况下,我已通过删除[object]来解决此警告.不需要关闭,但在这种情况下,我看不到需要更改的内容和

Now in other cases I've fixed this warning by removing an [object].Close that wasn't needed but in this case I can't see what needs to be altered and the help for the warning while being accurate isn't exactly informative as to how it is caused or how to fix it.

究竟是什么导致显示警告,我该如何重构以避免显示该警告?

What exactly is causing the warning to display and how can I refactor to avoid it?

''' <summary>
''' Serialize an object without adding the XML declaration, etc.
''' </summary>
''' <param name="target"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function SerializeElementToText(Of T As New)(target As T) As String
    Dim serializer As New XmlSerializer(GetType(T))
    'Need to serialize without namespaces to keep it clean and tidy
    Dim emptyNS As New XmlSerializerNamespaces({XmlQualifiedName.Empty})
    'Need to remove xml declaration as we will use this as part of a larger xml file
    Dim settings As New XmlWriterSettings()
    settings.OmitXmlDeclaration = True
    settings.NewLineHandling = NewLineHandling.Entitize
    settings.Indent = True
    settings.IndentChars = (ControlChars.Tab)
    Using stream As New StringWriter(), writer As XmlWriter = XmlWriter.Create(stream, settings)
        'Serialize the item to the stream using the namespace supplied
        serializer.Serialize(writer, target, emptyNS)
        'Read the stream and return it as a string
        Return stream.ToString
    End Using 'Warning jumps to this line
End Function

我尝试了此操作,但它也不起作用:

I tried this but it doesn't work either:

    Using stream As New StringWriter()
        Using writer As XmlWriter = XmlWriter.Create(stream, settings)
            serializer.Serialize(writer, target, emptyNS)
            Return stream.ToString
        End Using
    End Using 'Warning jumps to this line instead

推荐答案

这是一个错误的警告,由XmlWriter处置传递的流引起.这使您的StringWriter首先通过XmlWriter,然后通过Using语句处置两次.

It is a false warning, caused by XmlWriter disposing the stream you pass. Which makes your StringWriter disposed twice, first by XmlWriter and again by your Using statement.

这不是问题,两次处置.NET Framework对象不是错误,也不会造成任何麻烦.如果Dispose()方法的实现不佳,可能会成为一个问题,FxCop不会抓住机会不告诉您有关信息,因为它不那么聪明,无法知道Dispose()是否方法是正确的.

This is not a problem, disposing .NET framework objects twice is not an error and doesn't cause any trouble. It could be a problem if the Dispose() method is poorly implemented, FxCop doesn't take its chances to not tell you about it because it isn't otherwise smart enough to know if a Dispose() method is correct.

没有任何方法可以重写代码来避免出现警告. StringWriter实际上没有任何可处理的内容,因此可以将其移出Using语句.但这将产生另一个警告,CA2000.最好的办法就是忽略此警告.如果您不想再次查看它,请使用SuppressMessageAttribute.

There is not any way to rewrite the code to avoid a warning. StringWriter doesn't actually have anything to dispose so moving it out of the Using statement is okay. But that will produce another warning, CA2000. Best thing to do is to just ignore this warning. Use the SuppressMessageAttribute if you don't want to look at it again.

这篇关于是什么导致此代码中的"CA2202:请勿多次放置对象",我该如何重构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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