gpt4 book ai didi

.net - 如何在没有代码分析警告的情况下一起使用 StringWriter 和 HtmlWriter

转载 作者:行者123 更新时间:2023-12-04 00:41:40 26 4
gpt4 key购买 nike

我正在使用 .net 并且需要获取一些 html 文本,所以我想我会一起使用 HtmlTextWriter 和 StringWriter 来获取格式良好的 html。但是,尽管我编写代码的方式各不相同,但我仍然收到来自静态代码分析器(使用 Microsoft All Rules)的警告。在下面的代码示例中,我在注释中显示了代码分析器警告。为了简化代码,我实际上并没有对 HtmlTextWriter 进行任何调用(您将在每个函数中看到对此效果的注释)。如何正确编写代码以避免警告?

// CA2000 : Microsoft.Reliability : In method 'Default.Func1()', object 'stringWriter' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'stringWriter' before all references to it are out of scope.
public static string Func1()
{
string html;
StringWriter stringWriter;
using (var writer = new HtmlTextWriter(stringWriter = new StringWriter()))
{
// You would do some stuff with the writer here, but not for this example.

html = stringWriter.ToString();
}
return html;
}

// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in method 'Default.Func2()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 45
public static string Func2()
{
string html;
StringWriter stringWriter = null;
try
{
using (var writer = new HtmlTextWriter(stringWriter = new StringWriter()))
{
// You would do some stuff with the writer here, but not for this example.

html = stringWriter.ToString();
}
}
finally
{
if (stringWriter != null)
stringWriter.Dispose();
}
return html;
}

// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func3()'. To avoid generating a System.ObjectDisposedException
// you should not call Dispose more than one time on an object.: Lines: 61
public static string Func3()
{
string html;
using (var stringWriter = new StringWriter())
{
using (var writer = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.

html = stringWriter.ToString();
}
}
return html;
}

// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func4()'. To avoid generating a System.ObjectDisposedException you
// should not call Dispose more than one time on an object.: Lines: 77
public static string Func4()
{
string html;
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.

html = stringWriter.ToString();
}
}
return html;
}

// CA2202 : Microsoft.Usage : Object 'stringWriter' can be disposed more than once in
// method 'Default.Func5()'. To avoid generating a System.ObjectDisposedException you
// should not call Dispose more than one time on an object.: Lines: 100
public static string Func5()
{
string html;
StringWriter stringWriter = null;
try
{
stringWriter = new StringWriter();
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
// You would do some stuff with the writer here, but not for this example.

html = stringWriter.ToString();
}
}
finally
{
if (stringWriter != null)
stringWriter.Dispose();
}
return html;
}

最佳答案

将您的 Func5 修改为如下:

public static string Func5()
{
string html;
StringWriter stringWriter = null;
try
{
stringWriter = new StringWriter();
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
stringWriter = null;

// You would do some stuff with the writer here, but not for this example.

html = htmlTextWriter.InnerWriter.ToString();
}
}
finally
{
if (stringWriter != null)
stringWriter.Dispose();
}
return html;
}

关键是将stringWriter变量设置为null(不影响HtmlTextWriter实例的InnerWriter),然后使用InnerWriter.ToString()获取HTML。

这实际上只是之前评论中引用的 MSDN 文章中示例的修改版本,但专门适用于您的使用。

关于.net - 如何在没有代码分析警告的情况下一起使用 StringWriter 和 HtmlWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811383/

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