认真地读了李先生的这篇文章(http://blog.dreambrook.com/jzli/),也用.NET framework 1.1 试了三种情况。情况确实如此。我还用Ildasm.exe看了一下每个Parent的 Finalize()的元数据,如下:
protected void Finalize(){ Console.WriteLine("Parent.Finalize");} 的元数据如下:
.method family hidebysig instance void Finalize() cil managed
{
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldstr "Parent.Finalize"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Parent::Finalize
protected virtual void Finalize(){ Console.WriteLine("Parent.Finalize");} 的元数据如下:
.method family hidebysig newslot virtual
instance void Finalize() cil managed
{
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldstr "Parent.Finalize"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Parent::Finalize
~Parent(){ Console.WriteLine("Parent.~Parent");} 的元数据如下:
.method family hidebysig virtual instance void
Finalize() cil managed
{
// Code size 20 (0x14)
.maxstack 1
.try
{
IL_0000: ldstr "Parent.~Parent"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: leave.s IL_0013
} // end .try
finally
{
IL_000c: ldarg.0
IL_000d: call instance void Grandpapa::Finalize()
IL_0012: endfinally
} // end handler
IL_0013: ret
} // end of method Parent::Finalize
你可以比较一下三处灰色部分,也许这就是CLR会有三种不同结果的原因吧。有错,请您指正,谢谢。
如果真如李先生所言是个bug,难道去除这个bug就那么难嘛?