五、以自定义方式处理 这是有益的当你去考虑如果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); }
|
闂傚倷娴囬妴鈧柛瀣崌閺岀喖顢涘⿰鍐炬毉濡炪們鍎查崹鍧楀蓟閻旇 鍋撳☉娅亝鎱ㄩ崶褉鏀芥い鏇炴鐎氾拷闂傚倷绀侀幖顐ゆ偖椤愶箑纾块柛鎰嚋閼板潡鏌涘☉娆愮稇缂備讲鏅犻弻鐔碱敍濠婂喚鏆銈冨劵閹凤拷>>
正在阅读:C#中的异常处理(三)C#中的异常处理(三)
2004-02-14 09:34
出处:PConline
责任编辑:linjixiong
键盘也能翻页,试试“← →”键