处置,当它叫什么名字?叫什么名字、当它

2023-09-02 10:23:47 作者:Guetse(乱情)

考虑以下code:

 命名空间DisposeTest
{
    使用系统;

    类节目
    {
        静态无效的主要(字串[] args)
        {
            Console.WriteLine(调用测试);

            测试();

            Console.WriteLine(呼叫到完成测试);
        }

        静态无效测试()
        {
            DisposeImplementation迪=新DisposeImplementation();
        }
    }

    内部类DisposeImplementation:IDisposable的
    {
        〜DisposeImplementation()
        {
            Console.WriteLine(〜在DisposeImplementation实例称为);
        }
        公共无效的Dispose()
        {
            Console.WriteLine(处置在DisposeImplementation实例调用);
        }
    }
}
 

的Dispose只是从来没有得到的叫,就算我把一个等待循环的测试()之后; 调用。因此,相当多吸。我想写一类,它是简单,非常容易使用,以确保每一个可能的资源清理。我不希望把他们负有责任到我的类的用户。

可能的解决方法:使用使用,或者调用Dispose自己(basicly相同)。我可以强制用户使用使用?或者,我可以强制处置被称为?

调用 GC.Collect的(); 测试(); 也不起作用。

并没有援引任何处置。在拆解不工作,所以对象获取的解构,当它退出测试()

好球员,很明显现在!

感谢大家的答案!我将添加一个警告,在发表评论!

解决方案   

我想写一类,它是   简单,非常容易使用,   确保每一个可能的   资源清理。我不想   把该负有责任用户   我的课的。

dedecms中的文件管理器,里面的文件名如果是中文显示乱码,如何处理

您不能这样做。内存管理是根本就没有建立,以适应无能的开发者。

的IDisposable模式是用于开发作为告知对象的时候都用它做,而不是具有存储器管理试图可以认识到这一点通过使用像引用计数的一种方法

您可以使用终结一个后备谁不妥善处置对象的用户,但它不作为主要的方法很好地清理对象。工作顺利对象应适当设置,从而使更多的昂贵的终结并不永远需要被调用。

Consider the following code:

namespace DisposeTest
{
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Calling Test");

            Test();

            Console.WriteLine("Call to Test done");
        }

        static void Test()
        {
            DisposeImplementation di = new DisposeImplementation();
        }
    }

    internal class DisposeImplementation : IDisposable
    {
        ~DisposeImplementation()
        {
            Console.WriteLine("~ in DisposeImplementation instance called");
        }
        public void Dispose()
        {
            Console.WriteLine("Dispose in DisposeImplementation instance called");
        }
    }
}

The Dispose just never get's called, even if I put a wait loop after the Test(); invocation. So that quite sucks. I want to write a class that is straightforward and very easy to use, to make sure that every possible resource is cleaned up. I don't want to put that responsibilty to the user of my class.

Possible solution: use using, or call Dispose myself(basicly the same). Can I force the user to use a using? Or can I force the dispose to be called?

Calling GC.Collect(); after Test(); doesn't work either.

Putting di to null doesn't invoke Dispose either. The Deconstructor DOES work, so the object get's deconstructed when it exits Test()

Ok guys, it's clear now!

Thank you all for your answers! I will add a warning in the comment!

解决方案

I want to write a class that is straightforward and very easy to use, to make sure that every possible resource is cleaned up. I don't want to put that responsibilty to the user of my class.

You can't do that. The memory management is simply not built to accomodate clueless developers.

The IDisposable pattern is intended for developers as a way of telling an object when they are done with it, instead of having the memory management trying to figure that out by using things like reference counting.

You can use the Finalizer as a fallback for users who fail to dispose objects properly, but it doesn't work well as the primary method for cleaning up objects. To work smoothly objects should be disposed properly, so that the more costly Finalizer doesn't ever need to be called.