方法处置功能 C#

发布于 2024-11-19 19:20:08 字数 1128 浏览 11 评论 0原文

有没有办法在 C# 2010 (.Net 4) 中不使用 try-finally 块来处理方法变量?

故事: 我有几个文件处理程序方法。每个处理程序(方法)都可以在执行过程中中止。 我喜欢在退出方法之前处理所有方法的对象。 为每个处理程序编写 dispose 方法的选项不是一个选项。

谢谢

这展示了总体思路: `公共静态类比较 { 私有静态 bool CompareXmls(...) { //有7个对象根据4个条件在3个出口点被处理

        //General method flow:

        //1. init part of the object

        //2. if(!<condition1>)
                //disposed objects
                //exit method

        //3. perform some actions

        //4. init some of the other objects

        //2. if(!<condition2>)
                //disposed objects
                //exit method

        //and so on...
    }

    private static bool CompareJpgs(...)
    {
        //has 12 object to be disposed in 10 exit points acordding to 4 conditions
    }

    private static bool CompareBitMaps(...)
    {
        //has 5 object to be disposed in 3 exit points acordding to 4 conditions
    }

    private static bool CompareTxts(...)
    {
        //has 12 object to be disposed in 10 exit points acordding to 4 conditions
    }
}`

我还有另外7个比较方法

Is there a way to dispose method variables without using try-finally block in C# 2010 (.Net 4)?

The story:
I have few files handler methods. Each handler (method) can abort in the middle of execution.
I like to dispose all method's object before exiting the method.
The option to compose dispose method for each handler is not an option.

Thank you

This demonstrate the general idea:
`public static class Comparison
{
private static bool CompareXmls(...)
{
//has 7 object to be disposed in 3 exit points acordding to 4 conditions

        //General method flow:

        //1. init part of the object

        //2. if(!<condition1>)
                //disposed objects
                //exit method

        //3. perform some actions

        //4. init some of the other objects

        //2. if(!<condition2>)
                //disposed objects
                //exit method

        //and so on...
    }

    private static bool CompareJpgs(...)
    {
        //has 12 object to be disposed in 10 exit points acordding to 4 conditions
    }

    private static bool CompareBitMaps(...)
    {
        //has 5 object to be disposed in 3 exit points acordding to 4 conditions
    }

    private static bool CompareTxts(...)
    {
        //has 12 object to be disposed in 10 exit points acordding to 4 conditions
    }
}`

I have another 7 comparison methods

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

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

发布评论

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

评论(2

泅渡 2024-11-26 19:20:08

使用?有关如何以及为何使用 的完整说明,请参阅此处http://msdn.microsoft.com/en-us/library/yh598w02(v=VS.100).aspx。一个示例是

using (MyDisposableType disposable1 = GetDisposable1(), disposable2 = GetDisposable2() /*,...*/)
{
    //your actions
}

using 块退出后,其标头中声明的所有一次性对象都将被释放。

如果您的一次性对象属于不同类型,则可以使用嵌套 using

using (MyDisposableType1 disposable1 = GetDisposable1())
{
    using (MyDisposableType2 disposable2 = GetDisposable2())
    {
        //more usings if needed, and then your actions
    }
}

using? Refer here for the full description on how and why to use http://msdn.microsoft.com/en-us/library/yh598w02(v=VS.100).aspx. A sample would be

using (MyDisposableType disposable1 = GetDisposable1(), disposable2 = GetDisposable2() /*,...*/)
{
    //your actions
}

after exiting from the using block, all disposable objects declared in its header will be disposed.

If your disposable objects are of different types, you can use nested usings:

using (MyDisposableType1 disposable1 = GetDisposable1())
{
    using (MyDisposableType2 disposable2 = GetDisposable2())
    {
        //more usings if needed, and then your actions
    }
}
失而复得 2024-11-26 19:20:08

我会将该方法放在一个 idisposible 的对象上并调用finally 块中的 this.dispose()

I would put the method on an object that impiments idisposible and call this.dispose() in a finally block

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