gpt4 book ai didi

c# - MemoryStream、XmlTextWriter 和警告 4 CA2202 : Microsoft. 用法

转载 作者:数据小太阳 更新时间:2023-10-29 02:25:00 26 4
gpt4 key购买 nike

Visual Studio 2010 Ultimate 中的Run Code Analysis 命令在看到带有 MemoryStreamXmlTextWriter 的特定模式时返回警告。

这是警告:

Warning 7 CA2202 : Microsoft.Usage : Object 'ms' can be disposed more than once in method 'KinteWritePages.GetXPathDocument(DbConnection)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 421 C:\Visual Studio 2010\Projects\Songhay.DataAccess.KinteWritePages\KinteWritePages.cs 421 Songhay.DataAccess.KinteWritePages

这是表格:

static XPathDocument GetXPathDocument(DbConnection connection)
{
XPathDocument xpDoc = null;
var ms = new MemoryStream();
try
{
using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
{
using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
{

writer.WriteStartDocument();
writer.WriteStartElement("data");

do
{
while(reader.Read())
{
writer.WriteStartElement("item");
for(int i = 0; i < reader.FieldCount; i++)
{
writer.WriteRaw(String.Format("<{0}>{1}</{0}>", reader.GetName(i), reader[i].ToString()));
}
writer.WriteFullEndElement();
}

} while(reader.NextResult());

writer.WriteFullEndElement();
writer.WriteEndDocument();

writer.Flush();
ms.Position = 0;

xpDoc = new XPathDocument(ms);
}
}

}
finally
{
ms.Dispose();
}

return xpDoc;
}

此表单会产生相同类型的警告:

XPathDocument xpDoc = null;
using(var ms = new MemoryStream())
{
using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
{
using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
{
//...
}
}

}

return xpDoc;

顺便说一句,下面的表单会产生另一个警告:

XPathDocument xpDoc = null;
var ms = new MemoryStream();
using(XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
{
using(DbDataReader reader = CommonReader.GetReader(connection, Resources.KinteRssSql))
{
//...
}
}

return xpDoc;

以上产生警告:

Warning 7 CA2000 : Microsoft.Reliability : In method 'KinteWritePages.GetXPathDocument(DbConnection)', object 'ms' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'ms' before all references to it are out of scope. C:\Visual Studio 2010\Projects\Songhay.DataAccess.KinteWritePages\KinteWritePages.cs 383 Songhay.DataAccess.KinteWritePages

除了以下,我还有哪些选择?:

  • 取消警告 CA2202。
  • 抑制警告 CA2000 并希望微软正在处理MemoryStream(因为Reflector 没有向我显示源代码)。
  • 重写我的遗留代码以识别精彩的 XDocument 和 LINQ toXML。

最佳答案

首先,永远不要使用 new XmlTextWriter()。自 .NET 2.0 以来,它已被弃用。请改用 XmlWriter.Create()

其次,ms 的赋值应该在 using block 中:

using (var ms = new MemoryStream())
{
using (var writer = XmlWriter.Create(ms))
{
// ...
}
}

我相信警告是正确的。 MemoryStream 可以在释放 XmlTextWriter 时被释放,然后在“finally” block 中再次释放。

关于c# - MemoryStream、XmlTextWriter 和警告 4 CA2202 : Microsoft. 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4503794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com