为什么代码分析告诉我“不要多次处置对象"?这里: [英] Why does Code Analysis tell me, "Do not dispose objects multiple times" here:

查看:19
本文介绍了为什么代码分析告诉我“不要多次处置对象"?这里:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个代码:

public static string Base64FromFileName(string fileName){尝试{FileInfo fInfo = new FileInfo(fileName);long numBytes = fInfo.Length;FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);BinaryReader br = new BinaryReader(fStream);byte[] bdata = br.ReadBytes((int)numBytes);br.关闭();fStream.Close();返回 Convert.ToBase64String(bdata);}捕获(例外 e){扔e;}}

...我明白了,由 Visual Studio 的代码分析工具提供的警告,不要多次处理对象...为避免生成 System.ObjectDisposedException,您不应多次调用 DisposefStream.Close();"上的一个对象"线.

为什么?fStream 是否设置在上面那行关闭 BinaryReader 的地方?

无论如何,我这样重构它不是更好吗:

<预><代码>...使用 (FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)){使用 (BinaryReader br = new BinaryReader(fStream)){byte[] bdata = br.ReadBytes((int)numBytes);}//br.Close();}//fStream.Close();...

?

解决方案

BinaryReader.Close 也会关闭底层流,所以这确实会导致流被处理两次.但这不是真正的问题,处理两次并没有什么坏处.

你可以写得更好

using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))使用 (var br = new BinaryReader(fs, new UTF8Encoding(), true)){返回 Convert.ToBase64String(br.ReadBytes((int)numBytes));}

这是防弹版本:

  • 任何成功构建的东西都保证被处理
  • 您不会两次处理流,因为 BinaryReader 构造函数上的布尔值 leaveOpen 参数确保处理(关闭)它不会同时关闭流

On this code:

public static string Base64FromFileName(string fileName)
{
    try
    {
        FileInfo fInfo = new FileInfo(fileName);
        long numBytes = fInfo.Length;
        FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fStream);
        byte[] bdata = br.ReadBytes((int)numBytes);
        br.Close();
        fStream.Close();
        return Convert.ToBase64String(bdata);
    }
    catch(Exception e)
    {
        throw e;
    }
}

...I get, courtesy of Visual Studio's Code Analysis tool, the warning, "Do not dispose objects multiple times...To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object" on the "fStream.Close();" line.

Why? Is fStream disposed in the line above, where the BinaryReader is closed?

Wouldn't I be better off refactoring it like this anyway:

. . .
using (FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))   
{
    using (BinaryReader br = new BinaryReader(fStream)) 
    {
        byte[] bdata = br.ReadBytes((int)numBytes);
    } //br.Close();
} //fStream.Close();
. . .

?

解决方案

BinaryReader.Close also closes the underlying stream, so this would indeed cause the stream to be disposed of twice. But that's not a real problem, disposing twice doesn't hurt.

You could write this much better as

using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(fs, new UTF8Encoding(), true))
{
    return Convert.ToBase64String(br.ReadBytes((int)numBytes));
}

This is the bomb-proof version:

  • Anything that is successfully constructed is guaranteed to be disposed
  • You won't dispose of the stream twice because the boolean leaveOpen argument on the BinaryReader constructor ensures that disposing (closing) it won't also close the stream

这篇关于为什么代码分析告诉我“不要多次处置对象"?这里:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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