1. System.Object

 
     The runtime requires every type to ultimately be derived from the System.Object type.
 
     Because all types are ultimately derived from System.Object, you are guaranteed that every object of every type has a minimum set of methods. Specifically, the System.Object class offers the public instance methods listed in below.
 
     Equals
     GetHashCode
     ToString
     GetType
 
     In addition, types that derive from System.Object have access to the protected methods listed in below.
 
     MemberwiseClone
     Finalize
 
     The CLR requires all objects to be created using the new operator.
  
2. new
 
     Here’s what the new operator does:
 
     1. It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). 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. The bytes for these additional members are added to the size of the object.
 
     2. It allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these bytes are then set to zero (0). 
 
     3. It initializes the object’s type object pointer and sync block index members. 
 
     4. The type’s instance constructor is called, passing it any arguments (the string "ConstructorParam1" in the preceding example) specified in the call to new. Most compilers automatically emit code in a constructor to call a base class’s constructor. Each constructor is responsible for initializing the instance fields defined by the type whose constructor is being called. Eventually,  System.Object’s constructor is called, and this constructor method does nothing but return. You can verify this by using ILDasm.exe to load MSCorLib.dll and examine System.Object’s constructor method.
 
3. Casting Between Types
 
     One of the most important features of the CLR is type safety. At runtime, the CLR always knows what type an object is. You can always discover an object’s exact type by calling the GetType method. Because this method is nonvirtual, it is impossible for a type to spoof another type.
 
     The CLR allows you to cast an object to its type or to any of its base types.
 
     C# doesn’t require any special syntax to cast an object to any of its base types, because casts to base types are considered safe implicit conversions. However, C# does require the  developer to explicitly cast an object to any of its derived types since such a cast could fail at runtime.
     
     System.InvalidCastException
 
4. Castring with the C# as and is Operators
 
     The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false. The is operator will never throw an exception.
 
     The is operator is typically used as follows:
 
if (o is Employee) {
Employee e = (Employee) o;
// Use e within the remainder of the 'if' statement.
}
 
     The CLR’s type checking improves security, but it certainly comes at a performance cost, because the CLR must determine the actual type of the object referred to by the variable (o), and then the CLR must walk the inheritance hierarchy, checking each base type against the specified type (Employee). Because this programming paradigm is quite common, C# offers a way to simplify this code and improve its performance by providing an as operator
 
Employee e = o as Employee;
if (e != null) {
// Use e within the 'if' statement.
}
 
     Notice that the as operator causes the CLR to verify an object’s type just once. The as operator works just as casting does except that the as operator will never throw an exception. Instead, if the object can’t be cast, the result is null.
 
5. namespaces and assemblies
 
     using namespace;
 
     Be aware that a namespace and an assembly (the file that implements a type) aren’t necessarily related. In particular, the various types belonging to a single namespace might be implemented in multiple assemblies.
 
6. How things relate at Runtime
 
     Stack space is used for passing arguments to a method and for local variables defined within a method.
 
     All but the simplest of methods contain some prologue code, which initializes a method before it can start doing its work. These methods also contain epilogue code, which cleans up a method after it has performed its work so that it can return to its caller.
 
     When a method is called, the arguments will be pushed on the stack firstly,  then the address indicating where the called method should return to in the calling method is pushed on the stack.
 
     The Progress:
     
     (1) As the just-in-time (JIT) compiler converts method's Intermediate Language (IL) code into native CPU instructions, it notices all of the types that are referred to inside the method. At this time, the CLR ensures that the assemblies that define these types are loaded. Then, using the assembly’s metadata, the CLR extracts information about these types and creates some data structures to represent the types themselves.
 
     (2) All objects on the heap contain two overhead members: the type object pointer and the sync block index.
 
     (3) When you define a type, you can define static data fields within it. The bytes that back these static data fields are allocated within the type objects themselves. Finally, inside each type object is a method table with one entry per method defined within the type.
 
     (4) Whenever a new object is created on the heap, the CLR automatically initializes the internal type object pointer member to refer to the object’s corresponding type object.
 
     (5) Furthermore, the CLR initializes the sync block index and sets all of the object’s instance fields to null or 0 (zero) prior to calling the type’s constructor, a method that will likely modify some of the instance data fields. 
 
     (6) When calling a static method, the JIT compiler locates the type object that corresponds to the type that defines the static method. Then, the JIT compiler 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.
 
     (7) 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. JIT compiler walks down the class hierarchy toward Object looking for this method. It can do this because each type object has a field in it that refers to its base type. Then, the JIT compiler locates the entry in the type object’s method table that refers to the method being called, JITs the method (if necessary), and then calls the JITted code.
 
     (8) 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 that refers to the method being called, JITs the method (if necessary), and calls the JITted code.
 
     (9) Type objects are actually objects themselves, so they all contail type object pointer. When the CLR creates type objects, the CLR must initialize these members. 
 
     (10) When the CLR starts running in a processit immediately creates a special type object for the System.Type type (defined in MSCorLib.dll).
 
     (11) System.Type type object is an object itself and therefore also has a type object pointer member in it, and it is logical to ask what this member refers to. It refers to itself because the System.Type type object is itself an “instance” of a type object.
 
     (12) By the way, System.Object’s GetType method simply returns the address stored in the specified object’s type object pointer member. In other words, the GetType method returns a pointer to an object’s type object, and this is how you can determine the true type of any object in the system (including type objects).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

CLR via C# 3rd - 04 - Type Fundamentals的更多相关文章

  1. CLR via C# 3rd - 06 - Type and Member Basics

    1. Different Kinds  of Type Members        A type can define zero or more of the following kinds of ...

  2. CLR via C# 3rd - 05 - Primitive, Reference, and Value Types

    1. Primitive Types        Any data types the compiler directly supports are called primitive types. ...

  3. 4、Type fundamentals

    1.All Types Are Derived from System.Object The CLR requires all objects to be created using the new ...

  4. CLR via C# 3rd - 08 - Methods

       Kinds of methods        Constructors      Type constructors      Overload operators      Type con ...

  5. CLR via C# 3rd - 07 - Constants and Fields

    1. Constants        A constant is a symbol that has a never-changing value. When defining a constant ...

  6. CLR via C# 3rd - 03 - Shared Assemblies and Strongly Named Assemblies

    1. Weakly Named Assembly vs Strong Named Assembly        Weakly named assemblies and strongly named ...

  7. CLR via C# 3rd - 02 - Building, Packaging, Deploying, and Administering Applications and Types

    1. C# Compiler - CSC.exe            csc.exe /out:Program.exe /t:exe /r:MSCorLib.dll Program.cs       ...

  8. CLR via C# 3rd - 01 - The CLR's Execution Model

    1. Assemly       A managed module is a standard 32-bit Microsoft Windoes portable executable (PE32) ...

  9. CLR via C#(04)- 本是同根生

    一.等值性——Equals()方法 有时候我们需要比较两个对象是否相等,比如在一个ArrayList中进行排序查找等操作时. System.Object提供了Equals()虚方法: class Ob ...

随机推荐

  1. 用cmd命令行导数据

    Imp  hbtest/hbtest@moms file=d:/hj.dmp fromuser=moms touser=hbtest 也可以用PL/SQL Developer "工具&quo ...

  2. github上面建立分支

    首先是有一个github的账户,然后随便开个项目. 好了,现在把git命令行打开,输入下面几行代码. git clone https://github.com/user/repository.git ...

  3. SpringMVC 处理数据模型

    处理模型数据 Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 Map 及 M ...

  4. gradle环境配置、

    话不多说,直接上流程. 1.下载 gradle.zip文件,我以为已经为大家准备好了各个版本的下载地址. 链接: http://pan.baidu.com/s/1hqjIVlE 密码: 8ccb 本人 ...

  5. Java特性-Collection和Map

    创建博客的目的主要帮助自己记忆和复习日常学到和用到的知识:或有纰漏请大家斧正,非常感谢! 之前面试,被问过一个问题:List和Set的区别. 主要区别很明显了,两者都是数组形式存在的,继承了Colle ...

  6. C++多线程3

    #include "stdafx.h" #include <windows.h> #include <process.h> int g_count; ; u ...

  7. MVC教程相关

    本教程所有文章导航 本系列共10篇文章,翻译自Asp.Net MVC4 官方教程,由于本系列文章言简意赅,篇幅适中,从一个示例开始讲解,全文最终完成了一个管理影片的小系统,非常适合新手入门Asp.Ne ...

  8. Linux网络编程(简单的时间获取服务器)

    1.一个简单的服务器时间获取程序 服务器和客户端采用UDP通信的方式,来编写一个简单的时间获取应用. 把过程大致理顺一下,首先是服务器端的编写,使用的是迭代的方式,没有并发 先创建一个socket而后 ...

  9. 自顶而下设计FPGA

    对IC设计而言,FPGA设计层级大致包括:系统级和行为级,RTL级,门级和晶体管级.然而更普遍的情况,FPGA只是用作实时数据采集控制.某些快速处理算法.PCIe\DDR3等高速数据通道,甚至某些简单 ...

  10. 第二章 搭建Android开发环境

    这一章为我们讲解了如何搭建Android开发环境. 首先要了解的是Android底层开发需要哪些工具:搭建android应用程序开发环境.android NDK开发环境和交叉编译环境,前两个用来测试L ...