何时/如何调用IDisposable.Dispose? [英] When/how is IDisposable.Dispose called?

查看:206
本文介绍了何时/如何调用IDisposable.Dispose?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下实现了DisposeIDisposable.Dispose的类:

Given the following class which implements both Dispose and IDisposable.Dispose:

internal class DisposableClass : IDisposable
{
    public void Dispose()
    {
    }
    void IDisposable.Dispose()
    {
    }
}

当我调用DisposeableClass.Dispose(通过类的实例)时,public void Dispose称为:

When I make a call to DisposeableClass.Dispose (through an instance of the class), the public void Dispose is called:

DisposableClass newClass = new DisposableClass();
try
{
}
finally
{
    newClass.Dispose();
}

如果将try-finally最终更改为using语句,则将调用IDisposable.Dispose.

If you change the try-finally to a using statement, IDisposable.Dispose is called.

using (DisposableClass newClass = new DisposableClass())
{
}

但是NOT the IDisposable.Dispose which I defined.

编译器将using语句转换为((IDisposable)newClass).Dispose().完整的方法名显示它来自System.IDisposable.Dispose.

The compiler translates the using-statement to ((IDisposable)newClass).Dispose(). The full methodname shows it to be from System.IDisposable.Dispose.

IL_0014: callvirt instance void [mscorlib]System.IDisposable::Dispose()

何时将调用我的IDisposable.Dispose的自定义实现?

When will my custom implementation of IDisposable.Dispose be called?

请注意,我没有使用这个实际的实现,并且得到了不应使用的实现.但我仍然对何时调用哪个实现感到好奇.

Note that I am not using this actual implementation and I get that this should not be used. But I am still curious as to which implementation gets called when.

如果每个Dispose方法都有不同的实现;何时会调用哪种实现?

If I'd have a different implementation for each of the Dispose methods; which implementation would be called when?

推荐答案

您的处置方法是错误的.您应该查看处置模式了解如何正确执行此操作.

Your approach to disposing is wrong. You should look at The Dispose Pattern to understand how to do this properly.

但是... 回答您的问题,他们如何/为什么被称为...

However... To answer your question how / why are they called...

当您说newClass.Dispose();时,将调用您的public void Dispose(),因为它是您所要求的最佳匹配项".这样(并且不会太复杂)是因为它是层次结构中最高的,因此编译器希望您指的是它,因为它是最具体的.如果您还没有创建自己的方法,则可能需要遍历层次结构才能找到Dispose方法.

Your public void Dispose() is being called when you say newClass.Dispose(); because it is the best "match" for what you have asked. By that (and without getting too complicated) it is because it is the highest in the hierarchy and therefore the one the compiler expects you to mean because it is the most specific. If you hadn't created your own it would have gone through hierarchy to find a Dispose method.

使用using换行时,编译器会生成类似于以下代码:

When you wrap with using the compiler produces code similar to this:

DisposableClass newClass = new DisposableClass();
try
{
}
finally
{
    ((IDisposable)newClass).Dispose();
}

因此,它将显式调用IDiposable版本.

This will therefore call the IDiposable version explicitly.

更新

下面的全部工作示例将给出以下输出:

Full working sample below that will give this output:

Manually calling newClass.Dispose();
public void Dispose() being called.

Now wrapped in using...
void IDisposable.Dispose() being called.

Manually calling IDisposable.Dispose();
void IDisposable.Dispose() being called.

完整的工作代码(将其粘贴在控制台应用程序中并运行):

Full working code (paste this inside a console app and run):

using System;

namespace zPlayGround
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Manually calling newClass.Dispose();");
            var newClass = new DisposableClass();
            try
            {
            }
            finally
            {
                newClass.Dispose();
            }

            Console.WriteLine("Now wrapped in using...");
            using (var usingClass = new DisposableClass())
            {

            }

            Console.WriteLine("Manually calling IDisposable.Dispose();");
            var demoClass = new DisposableClass();
            try
            {
            }
            finally
            {
                ((IDisposable)newClass).Dispose();
            }
            Console.ReadKey();
        }
    }

    internal class DisposableClass : IDisposable
    {
        public void Dispose()
        {
            Console.WriteLine("public void Dispose() being called.\r\n");
        }
        void IDisposable.Dispose()
        {
            Console.WriteLine("void IDisposable.Dispose() being called.\r\n");
        }
    }
}

这篇关于何时/如何调用IDisposable.Dispose?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆