Hi,
We all know that Finalize is a protected method.But it can be called by CLR GC.
Can anyone explain this
Thanks in advance..
Visual C# Language
Peter is right, although to do that the code needs certain CodePermissions. Since code is often run as fully trusted this isn't usually an issue.
That said, I don't want you to think that the CLR GC is using reflection to call the finalizer. I'm pretty sure thats not the case. The reason that private/protected methods cannot be called is because the language compilers wont compile such code, and even if they did, the CLR will not allow the call when it JIT-compiles the code. However, the CLR can certainly allow itself to call protected/private methods, which is essentially what its doing when it calls the finalizer.
Yes, the CLR certainly doesn't use reflection to invoke the finalizers. Since the CLR is managing the vtables of methods for object, it inherently has access to that information. I was was just trying to show that being "protected" doesn't really offer a strict access guarantee.
Peter Ritchie wrote:
I was was just trying to show that being "protected" doesn't really offer a strict access guarantee.