I was browsing the decompiled source code for a DLL in Reflector, and I came across this C# code:
protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1)
{
if (flag1)
{
this.~ClassName();
}
else
{
base.Finalize();
}
}
My first reaction was "What? I thought you couldn't call the finalizer manually!"
Note: The base type is object.
To be sure it wasn't a Reflector quirk, I opened up the method in ILSpy. It generated similar code.
I went to Google to confirm my new discovery. I found the documentation for Object.Finalize, and this is what it said:
Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. This is the only case in which application code is allowed to call Finalize.
Now I don't what to think. It might be because the DLL was compiled with C++. (Note: I couldn't find the implementation of Dispose. Maybe it's auto-generated.) It might be a special allowance for the IDisposable.Dispose method. It might be a flaw in both decompilers.
Some observations:
- I couldn't find the implementation of Dispose in the source code. Maybe it's auto-generated.
- Reflector shows a method named
~ClassName. It seems as though this method might not actually be the finalizer, but the C++ destructor, or even an ordinary method.
Is this legal C#? If so, what is different about this case? If not, what is actually happening? Is it allowed in C++/CLI, but not C#? Or is it just a glitch in the decompiler?