正在阅读:C#中的异常处理(三)C#中的异常处理(三)

2004-02-14 09:34 出处:PConline 作者:ivsee/CSDN 责任编辑:linjixiong
  五、以自定义方式处理
 
  这是有益的当你去考虑如果TextReader没有实现IDisposable接口将会出现什么情况。这篇教程从此处开始将知道我们如何在我们自己的类里实现Dispose的处理。一种方式是使用对象适配器(Object Adapter)模式。例如:
  public sealed class AutoTextReader : IDisposable
  {
    public AutoTextReader(TextReader target)
    {
        // PreCondition(target != null);
        adaptee = target;
    }
 
    public TextReader TextReader
    {
        get { return adaptee; }
    }
 
    public void Dispose()
    {
        adaptee.Close();
    }
 
    private readonly TextReader adaptee;
  }
 
  你可以这样使用你自己的类:
 
  using (AutoTextReader scoped = new AutoTextReader(file.OpenText()))
  {
    scoped.TextReader.Read(source, 0, length);
 
  你能够使用隐式转换操作符使问题变得更简单一些:
  public sealed class AutoTextReader : IDisposable
  {
    ...
    public static implicit operator AutoTextReader(TextReader target)
    {
        return new AutoTextReader(target);
    }
    ...
  }
 
  这将允许你这样使用:
 
   using (AutoTextReader scoped = file.OpenText())
  {
    scoped.TextReader.Read(source, 0, length);
  }
 
  struct :另一种选择
 
  AutoTextReader有意使用为密封类,就想它的名字建议的,以用作本地变量。使用一个结构来代替类更加有意义:
 
  public struct AutoTextReader : IDisposable
  {
    // exactly as before
  }
 
  使用一个结构代替类也提供给你几种自由优化。既然一个结构是一个值类型,它能构一直都不是空值。这意味着编译器必须对生成的finally程序块做空值的检测。并且,既然你不能继承于一个结构,它的运行时将和编译时的类型一致。这意味着编译器一般在生成的finally程序块中做强制转换并因而避免了一次装箱操作(特别的,如果Dispose是一个公开的隐式接口实现而不是一个不公开的显示接口实现,这将避免强制转换)。
  换句话,就是这样:
 
  using (AutoTextReader scoped = file.OpenText())
  {
    scoped.TextReader.Read(source, 0, length);
  }
  被转换为:
  {
    AutoTextReader scoped = new file.OpenText();
    try
    {
        scoped.TextReader.Read(source, 0, length);
    }
    finally
    {
        scoped.Dispose();
    }
  }
 
  由此,我更喜欢使用using语句代替finally程序开来处理。事实上,using语句解决方式相较于开始的    “理想“版本还有如下额外的几个优点,一个using语句:
 
  ·运行中,它能够一直释放资源
 
  ·是一个扩展机制。它允许你创建一个资源释放的集合。创建你自己的资源释放类例如  AutoTextReader是容易的。
 
  ·允许你将资源获取和资源释放配对使用。释放资源最好的时刻就是你获得资源的一刻。就像如  果你从图书馆借了一本书,你能在你借的时候被告知什么时候归还。】
 
  ·根据句法构造,能够清楚的告诉你你正在使用一个资源。
 
  ·为拥有资源的变量创造一个范围。仔细观察对using语句的编译器转换,你将发现它聪明的使用了一对外部括号。
 
     using (AutoTextReader scoped = file.OpenText())
    {
          scoped.TextReader.Read(source, 0, length);
     }
     scoped.TextReader.Close(); // scoped is not in scope here
 
    这是对C++中依据条件声明的追溯。允许你限制变量使用的范围,仅仅在这个范围内,变量是有用的,当变量能够使用,它只能存在于这个范围内。

关注我们

最新资讯离线随时看 聊天吐槽赢奖品