Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HtmlTextWriter没有';垃圾处理后不会冲水吗?_C#_Dispose_Flush_Htmltextwriter - Fatal编程技术网

C# HtmlTextWriter没有';垃圾处理后不会冲水吗?

C# HtmlTextWriter没有';垃圾处理后不会冲水吗?,c#,dispose,flush,htmltextwriter,C#,Dispose,Flush,Htmltextwriter,我需要写一些有风格的文本(比如颜色、字体),所以我决定使用html。我发现HtmlTextWriter是一个用于编写html文件的类。但是,我发现我必须手动关闭或刷新它,否则不会向文件写入任何内容。为什么?(using语句应在块完成时进行处理) 提前感谢。您不需要在using语句中使用try{}Finally{}块,因为这将为您处理对象。这是htmltextwitter中的一个错误。您应该制作一个自包含的测试用例,并进行测试。似乎Close和Dispose的行为有所不同,这是没有文档记录的,也是

我需要写一些有风格的文本(比如颜色、字体),所以我决定使用html。我发现
HtmlTextWriter
是一个用于编写html文件的类。但是,我发现我必须手动关闭或刷新它,否则不会向文件写入任何内容。为什么?(using语句应在块完成时进行处理)


提前感谢。

您不需要在using语句中使用try{}Finally{}块,因为这将为您处理对象。

这是
htmltextwitter
中的一个错误。您应该制作一个自包含的测试用例,并进行测试。似乎
Close
Dispose
的行为有所不同,这是没有文档记录的,也是极不寻常的。我也找不到任何关于MSDN的文档,说明;i、 e.它会处理底层textwriter还是必须处理

编辑2:上的MSDN页面声明它继承(而不是重写)虚拟
Dispose(bool)
方法。这意味着当前的实现显然无法使用using块进行清理。作为解决方法,请尝试以下方法:

using(var writer = ...make TextWriter...) 
using(var htmlWriter = new HtmlTextWriter(writer)) {

    //use htmlWriter here...

} //this should flush the underlying writer AND the HtmlTextWriter

// although there's currently no need to dispose HtmlTextWriter since
// that doesn't do anything; it's possibly better to do so anyhow in 
// case the implementation gets fixed
顺便说一句,
newstreamwriter(XYZ,false,Encoding.UTF8)
相当于
newstreamwriter(XYZ)
。StreamWriter默认情况下创建而不是追加,并且默认情况下也使用不带BOM表的UTF8


祝你好运,别忘了

我怀疑原因是HtmlTextWriter没有为TextWriter的
受保护的虚拟void Dispose(bool disposing)
方法提供重写来调用
Close()
,因此,您是对的,您需要自己做这件事-TextWriter的实现是空的。正如aspect所指出的,您不需要在
using
语句中使用
try finally
块。正如Eamon Nerbonne所指出的,这肯定是一个框架缺陷。

谢谢。我从XMLTextWriter程序中复制了一些代码,我认为总是编写文档是个好主意。但我相信这不是什么大问题。谢谢你的建议。我认为html文档可能没有必要。非常感谢。这很有帮助。我只是对如何使用它有了一些基本的想法;我以前没有写过html。但我不熟悉报道的事情。也许你应该报告这个错误。(或者它是一个功能?)
using(var writer = ...make TextWriter...) 
using(var htmlWriter = new HtmlTextWriter(writer)) {

    //use htmlWriter here...

} //this should flush the underlying writer AND the HtmlTextWriter

// although there's currently no need to dispose HtmlTextWriter since
// that doesn't do anything; it's possibly better to do so anyhow in 
// case the implementation gets fixed