gpt4 book ai didi

c# - 如何创建使用类似于在 C# 中使用的配置文件的扩展方法

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:50 25 4
gpt4 key购买 nike

我正在尝试创建一个使用时如下所示的方法:

Dialog (var d = new MyDialog())
{
d.DoSomethingToAVisibleDialogThatAppearedInTheDialog(); //Call
}

像“using”一样,它将处理析构函数,但我想向 Dialog 添加更多内容,这将处理我的 IDialog 接口(interface)。

最佳答案

您可以创建如下类:

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
}

关于c# - 如何创建使用类似于在 C# 中使用的配置文件的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5708052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com