无法摆脱 CA2202 警告
Cannot get rid of CA2202 warning
我已阅读有关此的 MSDN 页面:https://msdn.microsoft.com/en-us/library/ms182334.aspx
还有这个 SO 答案:
但下面的代码仍然为我生成 2 个 CA2202 警告:
FileStream fileStream = null;
BufferedStream bufferedStream = null;
try
{
fileStream = File.Open(...);
bufferedStream = new BufferedStream(fileStream);
using (StreamReader streamReader = new StreamReader(bufferedStream))
{
...
}
}
finally
{
if (bufferedStream != null)
{
bufferedStream.Dispose();
}
if (fileStream != null)
{
fileStream.Dispose();
}
}
"bufferedStream.Dispose()"行仍然给出以下两个警告:
Severity Code Description Project File Line Suppression State
Warning CA2202 Object 'bufferedStream' can be disposed more than once in method 'Loader.UpdateIndex()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 930 Loader C:\Users\user\Loader\Loader.cs 930 Active
和
Severity Code Description Project File Line Suppression State
Warning CA2202 Object 'fileStream' can be disposed more than once in method 'Loader.UpdateIndex()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 930, 935 Loader C:\Users\user\Loader\Loader.cs 930 Active
大概是因为 fileStream 可以被多次处理?但是bufferedStream怎么能不止一次的处理呢?
编辑:我已将@Damien_The_Unbeliever 的回答标记为正确。您实际上也可以 trim 它,因为正如下面某处提到的,您不需要 bufferedReader 变量。这就是我最终得到的。它有点丑,但它有效:
FileStream fileStream = null;
try
{
fileStream = File.Open("iaushdiuh", FileMode.Open);
fileStream = null;
using (StreamReader streamReader = new StreamReader(fileStream))
{
streamReader.ReadLine();
}
}
finally
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
如果在将这些对象 "passed ownership" 发送给其他对象后,您不 使用 底层流,则可以像这样使警告静音:
FileStream fileStream = null;
BufferedStream bufferedStream = null;
try
{
fileStream = File.Open(...);
bufferedStream = new BufferedStream(fileStream);
fileStream = null;
using (StreamReader streamReader = new StreamReader(bufferedStream))
{
bufferedStream = null;
...
}
}
finally
{
if (bufferedStream != null)
{
bufferedStream.Dispose();
}
if (fileStream != null)
{
fileStream.Dispose();
}
}
您希望对 null
的赋值在一次性对象的 "takes ownership" 构造函数调用之后立即发生。通过这种方式,您可以确保如果构造函数抛出,您将处理内部对象并且如果构造函数成功然后它将安排处置发生。
我已阅读有关此的 MSDN 页面:https://msdn.microsoft.com/en-us/library/ms182334.aspx
还有这个 SO 答案:
但下面的代码仍然为我生成 2 个 CA2202 警告:
FileStream fileStream = null;
BufferedStream bufferedStream = null;
try
{
fileStream = File.Open(...);
bufferedStream = new BufferedStream(fileStream);
using (StreamReader streamReader = new StreamReader(bufferedStream))
{
...
}
}
finally
{
if (bufferedStream != null)
{
bufferedStream.Dispose();
}
if (fileStream != null)
{
fileStream.Dispose();
}
}
"bufferedStream.Dispose()"行仍然给出以下两个警告:
Severity Code Description Project File Line Suppression State Warning CA2202 Object 'bufferedStream' can be disposed more than once in method 'Loader.UpdateIndex()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 930 Loader C:\Users\user\Loader\Loader.cs 930 Active
和
Severity Code Description Project File Line Suppression State Warning CA2202 Object 'fileStream' can be disposed more than once in method 'Loader.UpdateIndex()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 930, 935 Loader C:\Users\user\Loader\Loader.cs 930 Active
大概是因为 fileStream 可以被多次处理?但是bufferedStream怎么能不止一次的处理呢?
编辑:我已将@Damien_The_Unbeliever 的回答标记为正确。您实际上也可以 trim 它,因为正如下面某处提到的,您不需要 bufferedReader 变量。这就是我最终得到的。它有点丑,但它有效:
FileStream fileStream = null;
try
{
fileStream = File.Open("iaushdiuh", FileMode.Open);
fileStream = null;
using (StreamReader streamReader = new StreamReader(fileStream))
{
streamReader.ReadLine();
}
}
finally
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
如果在将这些对象 "passed ownership" 发送给其他对象后,您不 使用 底层流,则可以像这样使警告静音:
FileStream fileStream = null;
BufferedStream bufferedStream = null;
try
{
fileStream = File.Open(...);
bufferedStream = new BufferedStream(fileStream);
fileStream = null;
using (StreamReader streamReader = new StreamReader(bufferedStream))
{
bufferedStream = null;
...
}
}
finally
{
if (bufferedStream != null)
{
bufferedStream.Dispose();
}
if (fileStream != null)
{
fileStream.Dispose();
}
}
您希望对 null
的赋值在一次性对象的 "takes ownership" 构造函数调用之后立即发生。通过这种方式,您可以确保如果构造函数抛出,您将处理内部对象并且如果构造函数成功然后它将安排处置发生。