在C#中使用(OBJ的IDisposable =新...)写在流代码块(例如XML) [英] using(IDisposable obj = new ...) in C# to write code blocks in stream (e.g. XML)

查看:103
本文介绍了在C#中使用(OBJ的IDisposable =新...)写在流代码块(例如XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用实现IDisposable接口的类来写块流,与using语句。这有利于保持正确的嵌套和避免遗漏或错误放置的开始/结束的部分。



基本上,构造函数写一个块的开始(例如,开放的XML标记) ,Dispose()方法结束(如XML结束标记)。例如低于UsableXmlElement(这对大型XML的,所以LINQ到内存中的XML或XmlDocument的是没有选项)。



不过,这些IDisposable的不实现建议的复杂的模式由微软与析构函数/终结,独立的Dispose(布尔)方法和GC.SuppressFinalize()。处置只是将结束元素,这就是它。



有什么不好的一面这一点,或者这是保持要素的正确嵌套的好办法?

 类UsableXmlElement:IDisposable的
{
私人的XmlWriter _xwriter;

公共UsableXmlElement(字符串名称,XMLWriter的的XmlWriter)
{
_xwriter = XmlWriter的;
_xwriter.WriteStartElement(名);
}

公共无效WriteAttribute< T>(字符串名称,T值)
{
_xwriter.WriteStartAttribute(名);
_xwriter.WriteValue(值);
_xwriter.WriteEndAttribute();
}

公共无效WriteValue< T>(T值)
{
_xwriter.WriteValue(值);
}

公共无效的Dispose()
{
_xwriter.WriteEndElement();
}
}



用法是这样的:

  VAR xWriter =新的XmlWriter(...)

使用(VAR rootElement的=新UsableXmlElement(rootElement的xWriter)
{
使用
rootElement.WriteAttribute(DocVersion,123)(VAR innerElement =新UsableXmlElement(InnerElement,xwriter)
{
//写什么内部元件
}
}

致使里面的:

 < rootElement的DocVersion =123> 
< InnerElement>
<! - 什么 - > ;
< / InnerElement>
< / rootElement的>


解决方案

主要缺点我看到的(除了不规范使用using语句可以说是违反了最小惊讶原则)的是,它会试图反复写入所有嵌套的结束标记在你的的XmlWriter



引发的异常的事件

在至少从理论上讲,你可以有,而写作抛出的异常内结束标记,然后通过using语句所产生的最后块外端标签成功写入。这将导致无效的输出。


I have started to use classes implementing IDisposable to write blocks in streams, with the using statement. This is helpful to keep a correct nesting and avoid missing or wrongly placed start/end parts.

Basically, the constructor writes the start of a block (e.g. opening XML tag), Dispose() the end (e.g. closing XML tag). Example is the UsableXmlElement below (it's for large XMLs, so LINQ to XML or XmlDocument in memory are no options).

However, these IDisposable do not implement the sophisticated pattern recommended by Microsoft, with Destructor/Finalizer, separate Dispose(bool) method and GC.SuppressFinalize(). Dispose simply writes the end element, and that's it.

Is there any down side to this, or is this a good way to maintain a correct nesting of elements?

class UsableXmlElement : IDisposable
{
    private XmlWriter _xwriter;

    public UsableXmlElement(string name, XmlWriter xmlWriter)
    {
        _xwriter = xmlWriter;
        _xwriter.WriteStartElement(name);
    }

    public void WriteAttribute<T>(string name, T value)
    {
        _xwriter.WriteStartAttribute(name);
        _xwriter.WriteValue(value);
        _xwriter.WriteEndAttribute();
    }

    public void WriteValue<T>(T value)
    {
        _xwriter.WriteValue(value);
    }

    public void Dispose()
    {
        _xwriter.WriteEndElement();
    }
}

Usage is like this:

var xWriter = new XmlWriter(...)

using(var rootElement = new UsableXmlElement("RootElement", xWriter)
{
   rootElement.WriteAttribute("DocVersion", 123)
   using(var innerElement = new UsableXmlElement("InnerElement", xwriter)
   {
       // write anything inside Inner element
   }
}

Resulting in:

<RootElement DocVersion="123">
    <InnerElement>
       <!-- anything -->
    </InnerElement>
</RootElement>

解决方案

The main downside I see (apart from the non-standard use of the using statement arguably violating the "principle of least surprise") is that it will attempt to repeatedly write all the nested end tags in the event of an exception thrown by your XmlWriter.

In theory at least, you could have an exception thrown while writing an inner end tag, followed by successful writes of outer end tags in the "finally" blocks generated by the using statements. This would lead to invalid output.

这篇关于在C#中使用(OBJ的IDisposable =新...)写在流代码块(例如XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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