对象的处置多次 [英] Disposing of object multiple times

查看:184
本文介绍了对象的处置多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,它采用了流打开和修改Open XML文档,然后保存该流的新的二进制表示法:

I have the following code, which uses a stream to open and modify an Open XML document, and then save the new binary representation of that stream:

MemoryStream stream = null;
try
{
    stream = new MemoryStream();
    stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length);

    using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true))
    {
        OfficeDocument.ModifyDocument(document);
        this.SetBinaryRepresentation(stream.ToArray());
        stream = null;
    }
}
finally
{
    if (stream != null)
    {
        stream.Dispose();
    }
}



本来我用了两个使用块(一个用于MemoryStream的第二为WordprocessingDocument),却收到警告CA2202:对象流可以设置比方法一次多......每的 MSDN文章,我修改了代码上面(转换外使用一试),但我仍然收到此警告。

I had originally used two using blocks (one for the MemoryStream and the second for the WordprocessingDocument), but received warning CA2202: "Object 'stream' can be disposed more than once in method..." Per the MSDN article, I modified the code to above (converting the outer using to a try), but I am still receiving this warning.

我不确定我怎样才能构建这个方法来确保调用Dispose流上的一次。我宁愿因为MSDN文章指出,你不应该依赖于被处置安全调用多次没有简单地禁止这种警告。

I'm unsure of how I can structure this method to ensure that Dispose is called exactly once on the stream. I would prefer not to simply suppress this warning since the MSDN article states that you shouldn't rely on Dispose being safely callable multiple times.

推荐答案

这从MSDN文章的例子对你没有工作的原因是,他们设置了流,尽快为他们进入使用块空,而你用你使用块内的流并设置流之后为null。如果一个异常是你的流之前抛出= NULL 语句,将被布置为using块退出的,然后在你的finally块。

The reason that the example from the MSDN article did not work for you is that they set the stream to null as soon as they enter the using block, whereas you use the stream inside your using block and set the stream to null after. If an exception is thrown before your stream = null statement, stream would be disposed of as the using block is exited, and then again in your finally block.

不幸的是,因为你需要文件后访问您的流更新了它,我没有看到一个清洁的方式使用自己的设定流= NULL 您的使用语句内,以避免多个的Dispose()来电。另一种方法是你可以同时声明文件 try块外面,然后清理他们两个你的最后,像这样里面的:

Unfortunately, since you need to access your stream after document has updated it, I don't see a clean way to use their example of setting stream = null within your using statement to avoid the multiple Dispose() calls. An alternative would be to you could declare both stream and document outside of the try block, and then clean both of them up inside your finally, like so:

MemoryStream stream = null;
WordprocessingDocument document = null;
try
{
    stream = new MemoryStream();
    stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length);

    document = WordprocessingDocument.Open(stream, true));

    OfficeDocument.ModifyDocument(document);
    this.SetBinaryRepresentation(stream.ToArray()); 
}
finally
{
    if( document != null)
    {
        document.Dispose();
    }
    // Catch the case where an error occurred before document was defined.
    else
    {
        stream.Dispose();
    }
}

这篇关于对象的处置多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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