All Types Are Derived from System.Object
Every object on the heap requires some additional members—called the type object pointer and the sync block index—used by the CLR to manage the object .
When new a object:
- It calculates the number of bytes required. Include all instance fields defined in the type and all of its base types , object pointer and the sync block index.
- It allocates memory from the managed heap; all of these bytes are then set to zero (0) .
- It initializes the object’s type object pointer and sync block index members .
- The type’s instance constructor and base class’s constructoris called.
Casting Between Types
The CLR allows you to implicitly cast an object to its type or to any of its base types , and does require the developer to explicitly cast an object to any of its derived types.
is and as Operators will never throw an exception .
if (o is Employee) {
Employee e = (Employee) o;
// Use e within the remainder of the 'if' statement.
}
In this code, the CLR is actually checking the object’s type twice. Can improve its performance by :
Employee e = o as Employee;
if (e != null) {
// Use e within the 'if' statement.
}
Namespaces and Assemblies
The CLR doesn’t know anything about namespaces . When you access a type, the CLR needs to know the full name of the type.A namespace and an assembly (the file that implements a type) aren’t necessarily related .
How Things Relate at Runtime
When a thread is created, it is allocated a 1-MB stack . This stack space is used for passing arguments to a method and for local variables defined within a method .
Local variables are allocating on the thread’s stack.
When calling a method a method pushes arguments and the return address on the thread’s stack .
The CLR extracts information about referred types and creates some data structures to represent the types themselves . The static data fields are allocated within the type objects themselves . Inside each type object is a method table with one entry per method defined within the type .
When calling a static method, the JIT compiler locates the type object that corresponds to the type that defines the static method and locates the entry in the type object’s method table that refers to the method being called,JITs the method (if necessary), and calls the JITted code .
When calling a nonvirtual instance method, the JIT compiler locates the type object that corresponds to the type of the variable being used to make the call and walks down the class hierarchy toward Object looking for this method . Then, the JIT compiler locates the entry in the type object’s method table.
When calling a virtual instance method, the JIT compiler produces some additional code in the method, which will be executed each time the method is invoked . This code will first look in the variable being used to make the call and then follow the address to the calling object . Then, the code will examine the object’s internal type object pointer member; this member refers to the actual type of the object .The code then locates the entry in the type object’s method table.
浙公网安备 33010602011771号