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

发布于 2024-11-01 19:45:37 字数 223 浏览 0 评论 0原文

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

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

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

I am trying to create a method that looks like the following when used:

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

Like "using", it would handle the destructor, but I want to add more stuff to Dialog, which would handle my IDialog interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归途 2024-11-08 19:45:37

您可以创建一个如下所示的类:

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
}

You could created a class like the following:

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
        }
    }
}

And then use it like so:

using (var wrapper = new DisposableWrapper(new Dialog()) {
    Dialog d = wrapper.Item;
    // do stuff
}
笑看君怀她人 2024-11-08 19:45:37

using 是一项语言功能,特定于 IDisposable。它不能直接扩展为不同的语义。您所尝试的基本上是向该语言添加新功能,这是不可能的。

最好的选择通常是提供一个操作,最好是使用带有 new() 约束的通用方法。

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());

using is a language feature, and specific to IDisposable. It cannot be extended directly for different semantics. What you're trying would basically be adding a new feature to the language, which is not possible.

The best option is typically to provide an action, ideally in a generic method with a new() constraint.

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
       }
    }
}

You could then do:

Dialog(d => d.DoSomethingToAVisibleDialogThatAppearedInTheDialog());
月牙弯弯 2024-11-08 19:45:37

我编写了一个名为 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

I wrote up a class that does this called ResourceReleaser<T>.

Typical usage:

public class StreamPositionRestorer : ResourceReleaser<long>
{
    public StreamPositionRestorer(Stream s) : base(s.Position, x => s.Seek(x)) { }
}

the end result of this example is that when you do this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文