System.Object 类中的 Finalize 方法

发布于 2024-08-19 03:09:46 字数 507 浏览 7 评论 0原文

出于好奇,我反汇编了 mscorlib.dll 以检查 System.Object 类的实现。

我发现了一些奇怪的东西。

1).    
public class Object {
...
    protected override void Finalize(){}
...
}

为什么基类中有重写方法?

2) public class Employee {
            public void InstanceMethod() {
                this.Finalize();
                //Does not compile, can i not access protected methods of base class??
            }
        }

我只是想知道 Object 类中“受保护的 Finalize”方法有什么用以及为什么编译器会对其进行特殊处理?

Out of curiosity i disassembled mscorlib.dll to check the implementation of System.Object class.

I found something weird in that.

1).    
public class Object {
...
    protected override void Finalize(){}
...
}

How come a base class has an overriden method in it?

2) public class Employee {
            public void InstanceMethod() {
                this.Finalize();
                //Does not compile, can i not access protected methods of base class??
            }
        }

I am just wondering what's the use of "protected Finalize" method in Object class and why it has got special treatment by compiler?

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

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

发布评论

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

评论(3

对岸观火 2024-08-26 03:09:46

这是 Reflector 中的一个错误,它会被虚拟但没有“newslot”属性并且没有基类类型的方法混淆。当你将反编译器切换到IL时可能会更容易看到。

终结器的真实声明(从参考源复制而来)与您所期望的一样:

// Allow an object to free resources before the object is reclaimed by the GC.
//
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
~Object()
{
}

It is a bug in Reflector, it gets confused by a method that's virtual but doesn't have the "newslot" attribute and doesn't have a base class type. It might be easier to see when you switch the decompiler to IL.

The real declaration of the finalizer, as copied from the Reference Source, is much as you'd expect it to be:

// Allow an object to free resources before the object is reclaimed by the GC.
//
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
~Object()
{
}
软甜啾 2024-08-26 03:09:46

对于第二个问题,C#的~MyClass在VB.NET中编写为Protected Overrides Sub Finalize(),相当于protected override Finalize() >。所以这只是 C# 语法的差异。

对于第一个问题,在 Reflector 中,

.method family hidebysig virtual instance void Finalize() cil managed

与被覆盖的虚拟成员相比,它缺少通常在新虚拟成员上看到的 newslot 属性。

For the second question, C#'s ~MyClass is written in VB.NET as Protected Overrides Sub Finalize() which is equivalent to protected override Finalize(). So it is just C# syntax difference.

For the first question, in Reflector it is

.method family hidebysig virtual instance void Finalize() cil managed

which is missing newslot attribute generally seen on new virtual members as compared to overridden.

無處可尋 2024-08-26 03:09:46

查看 MSDN 中的 Object.Finalize

析构函数是用于执行清理操作的 C# 机制。析构函数提供适当的保护措施,例如自动调用基类型的析构函数。在 C# 代码中,无法调用或覆盖 Object.Finalize。

因此,您的问题的答案是:嗯 - 这是 CLR 内部的一部分; C# 编译器会完成编写时所需的所有工作,例如:

public class Employee
{
   //Finalizer, also known as "destructor"
   ~Employee()
   {

   }
}

Check out the MSDN to Object.Finalize:

Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type's destructor. In C# code, Object.Finalize cannot be called or overridden.

Therefore, an answer to your question would be: Well - that's part of the internals of the CLR; the C# compiler does all the work required when writing for example:

public class Employee
{
   //Finalizer, also known as "destructor"
   ~Employee()
   {

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