This is a mirror of official site: http://jasper-net.blogspot.com/

Subterranean IL: Virtual methods and callvirt

| Sunday, November 7, 2010
Next up is a look at the details of callvirt and what happens when you call a virtual method. However, in order to do that, we first need to understand some of the low-level CLR datastructures, and what exactly an object instance is.

Object instances

Object instances are actually suprisingly small and lightweight things - they comprise of a sync block index used for locking on the object, a pointer to an internal MethodTable structure corresponding to the type that this object is an instance of, and the instance fields. That's it. Everything else - method implementations, interfaces, supertype information - is part of the MethodTable structure for the type (this is the structure pointed to by Type.TypeHandle). And, most importantly, within the MethodTable structure is stored all the method and interface information used for dynamic method dispatch through callvirt.

MethodTable

Within each MethodTable structure is a list of all the methods on the type, including inherited methods, in inheritance order. The first entries are always the four virtual methods on System.Object (ToString, Equals, GetHashCode and Finalize), followed by the virtual methods on the next supertype, and so on, finishing off with virtual and non-virtual methods declared on the type itself. Each entry in the table points to its IL implementation, which can either be its own overriding implementation or the same implementation as its supertype. For example, the following class definitions:

public class ClassA {
   public virtual void Method1() { /* ... */ }
}
public class ClassB : ClassA {
   public virtual void Method2() { /* ... */ }
}

will result in MethodTable method lists looking something like this:

Read more: Simple-talk

Posted via email from .NET Info

0 comments: