问题描述
||
我正在尝试创建一种使用时如下所示的方法:
Dialog (var d = new MyDialog())
{
d.DoSomethingToAVisibleDialogThatAppearedInTheDialog(); //Call
}
像\“ using \”一样,它将处理析构函数,但我想向Dialog添加更多内容,以处理我的IDialog接口。
解决方法
您可以创建一个如下所示的类:
的类。
典型用法:
class DisposableWrapper<T> : where T : IDisposable {
T _wrapped;
private bool _disposed;
public DisposableWrapper(T wrapped) {
_wrapped = wrapped;
}
public T Item {
get { return _wrapped; }
}
public ~DisposableWrapper()
{
Dispose(false);
GC.SuppressFinalize(this);
}
public void Dispose() {
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) {
_disposed = true;
((IDisposable)_wrapped).Dispose();
// do extra stuff
}
}
}
然后像这样使用它:
using (var wrapper = new DisposableWrapper(new Dialog()) {
Dialog d = wrapper.Item;
// do stuff
}
,using
是一种语言功能,特定于IDisposable
。无法针对不同的语义直接扩展它。您要尝试的基本上是为该语言添加新功能,而这是不可能的。
最好的选择通常是提供一个动作,理想情况下是采用约束为“ 5”的通用方法。
public static void Dialog<T>(Action<T> action) where T: IDialog,new()
{
var d = new T();
try
{
action(d);
}
finally
{
var idialog = d as IDialog;
if (idialog != null)
{
idialog.Dispose(); // Or whatever IDialog method(s) you want
}
}
}
然后,您可以执行以下操作:
Dialog(d => d.DoSomethingToAVisibleDialogThatAppearedInTheDialog());
,我编写了一个称为ResourceReleaser public class StreamPositionRestorer : ResourceReleaser<long>
{
public StreamPositionRestorer(Stream s) : base(s.Position,x => s.Seek(x)) { }
}
此示例的最终结果是,当您执行此操作时:
using (var restorer = new StreamPositionRestorer(stm)) {
// code that mucks with the stream all it wants...
}
// at this point the stream position has been returned to the original point