gpt4 book ai didi

c# - System.Object 类中的 Finalize 方法

转载 作者:行者123 更新时间:2023-11-30 15:11:42 26 4
gpt4 key购买 nike

出于好奇,我反汇编了 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 类中“protected Finalize”方法有什么用,为什么它得到编译器的特殊处理?

最佳答案

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

从 Reference Source 复制的终结器的真实声明与您期望的一样:

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

关于c# - System.Object 类中的 Finalize 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com