I know that when objects are created in Heap, they also have extra two more fields:
- Sync Block Index
- Type Object Pointer
So I wonder when Type Object is created in Heap memory and what kind of data it holds? It only represents the metadata of the Type?
I haven’t been able to find much detail about that.
The Type object also contains the bytes that back any static fields on the type as well as a method table with one entry per method defined within the type.
Each entry in the method table points to JIT-compiled native code if the method has been executed at least once.
The type object is created the first time the type is instantiated or the first time a static type member is referenced.
I highly recommend buying a copy of Jeffrey Richter’s book, CLR via C# if you want to get a really deep understanding of what the CLR does. The section titled “How Things Relate at Runtime” in chapter 4 covers the .NET type allocation process on the heap in detail.
The May 2005 edition of MSDN magazine has an article titled “JIT and Run: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects” with some good information as well, specifically the sections on Type Fundamentals and MethodTable.