CLR via C # Deep solution note two-type design

Source: Internet
Author: User

Type BasicsAll types derive from System.Object the CLR requires all objects to be created with the new operator. Employee E = new Employee ("Constructor Parameters"); Below are new operatorWhat to do: #1, the number of bytes required for all instance fields defined in the calculation type and all base types (until System.Object, although it does not define its own instance field). Each object on the heap also requires some additional members of the overhead cost member-that is, the type object pointer and the sync block index. These members are used by the CLR to manage objects. The number of bytes for these extra members is counted into the object size. #2, it allocates the memory of the object by allocating the number of bytes required by the specified type from the managed heap, and all allocated bytes are set to 0. #3 that initializes the object's type object pointer and synchronized block index members. #4, invokes the instance constructor of the type and passes it to any arguments specified in the call to new. Most compilers automatically generate code in the constructor to invoke a base class constructor. Each type of constructor, when called, is responsible for initializing the instance fields defined by this type. The most general call to System.Object is the constructor, which simply returns and does nothing else. After all the actions have been performed, a reference (or pointer) to the new object is returned. In the preceding example code, the reference is saved to the variable E, which has the employee type. " new instance" and "instance member" of the class: Two different "instances". One is an instance of a class, that is, a specific object. The other is an instance field defined in the class. The so-called "instance field" refers to non-static fields, sometimes referred to as "instance members." In short, instance members are objects belonging to classes, while static members belong to classes. type safetyThe CLR always knows what type an object (an instance of a type) is. All expressions are parsed into an instance of a type, and in the compiler-generated code, only the operations that are valid for that type are executed. The advantage of a type-safe language compared to non-type-safe languages is that many of the errors that programmers make can be detected at compile time, ensuring that the code is correct before you try to execute it. In addition, compile-time languages often generate smaller, faster code because they can make more assumptions at compile time and implement those assumptions in the generated IL and metadata. Type ConversionsOne of the most important features of the CLR is type safety. At run time, the CLR always knows what type an object is. Call the GetType () method and always know what the exact type of an object is. In development, developers often convert an object from one type to another. The CLR allows an object to be converted to its (actual) type or to any of its base types. #1: Conversions to a base type are considered to be a safe, implicit conversion. #2: When you convert an object to one of its derived types, C # requires that only explicit conversions be possible, because such conversions may fail at run time. At run time, the CLR checks the type operation to determine whether the object is always converted to the actual type or any of its base types.   This is the type-safe design. If the CLR allows such transformations, there is no type of security, and unpredictable results will occur-including application crashes and the advent of security vulnerabilities (because one type can easily be disguised as another). Type spoofing is the source of many security vulnerabilities, and it can also disrupt the stability and robustness of your application. Type safety is an important goal for the CLR. The  is operator checks whether an object is compatible with the specified type and returns a Boolean value, either True or false. Note that the IS operator never throws an exception. If the object reference is Null,is the operator always returns false.  object o = new Object (); if (O is Employee) {     employee e = (Employee) o;   In this code, the CLR actually checks the type of the two-second object. The CLR's type checking enhances security, but undoubtedly has a certain impact on performance. Because the CLR must first determine the actual type of the object that the variable refers to. The CLR must then traverse the inheritance hierarchy and use each base type to check for the specified type (such as employee). The above is a fairly common programming pattern, so C # specializes in providing the as operator, which is designed to simplify the way this code is written, while improving performance.  employee e = o as employee;if (E! = null) {     //...} The  as operator works the same way as coercion type conversions, except that it never throws an exception--conversely, if the object cannot be transformed, the result is null. So, the right thing to do is to check if the final generated reference is null. You should not use the final generated reference directly, or you might throw a System.NullReferenceException exception.  Note: C # allows you to define a conversion operator method in a type. These methods are called only when a transformation expression is used, and they are never called when using the As or is operators in C #.   Namespaces (namespace)Used to logically group related types, developers can use namespaces to conveniently locate a type. For example, the System.Text namespace defines a set of types that perform string processing. The using directive instructs the compiler to append a different prefix for each type until a match is found. Using use, not only greatly reduces the amount of typing, but also helps to enhance the readability of the code. The using directive also supports another form, allowing aliases to be created for a type or namespace. If you want to use only a few types in a namespace, and you don't want all of its types to run out of "polluting" the global namespace, aliases are handy. Using system;using Jack = CSI. Widgets; Important : The CLR does not know anything about the namespace. When you access a type, the CLR needs the full name of the type, which may be a fairly long name that contains a period symbol, and which assembly the definition of the type is specific to. In this way, the runtime can load the correct assembly, locate the target type, and manipulate it. The compiler scans all referenced assemblies and finds the definition of the type in it. Once the correct assembly is found, the assembly information and type information are embedded in the metadata of the resulting managed module. In order to obtain assembly information, the assembly that defines the referenced type must be passed to the compiler. By default, the C # compiler automatically looks for "referenced types" in the MSCorLib.dll assembly, even if you do not explicitly tell it to do so. The MSCorLib.dll assembly contains definitions for all core Framework class library (FCL) types, such as Object, Int32, string, and so on. Namespaces and assemblies (implementations of a type of file) are not necessarily relevant. In particular, each type in the same namespace may be implemented in a different assembly. In an assembly, it may also contain types in different namespaces. the relationship between the run timeThe relationships between types, objects, line stacks, and managed heaps at run time.  The difference between calling a static method, an instance method, and a virtual method. A Microsoft Windows process for the CLR has been loaded. There may be multiple threads in this process. When a thread is created, it is assigned to a 1MB-sized stack. The space for this stack is used to pass arguments to the method and is used for local variables defined inside the method. The stack is built from a high memory address to a low memory address. Stack Frame(Stack frame) represents a method call in the call stack of the current thread. Calls to each method made during thread execution are created in the call stack and pressed into a stack frame. We discussed the relationship between source code, IL, and JIT-compiled code, and also discussed line stacks, arguments, local variables, and how those arguments and variables refer to objects on the managed heap. I got it. The object contains a pointer to the object's type object (the Type object contains static fields and a party publication). It also discusses how the JIT compiler determines the methods of static, non-virtual, and actual instances. This understanding of all can help to profoundly understand how the CLR works. Note that both the employee and manager type objects contain the type Object pointer member. This is because the type object is also an object in nature. These members must be initialized when the CLR creates type objects. What does it initialize? When the CLR starts running in a process, it immediately creates a special type object for the System.Type type defined in MSCorLib.dll. Employee and manager type ObjectAre "instances" of that type.  Therefore, their type object pointer members are initialized to a reference to the System.Type type object, as shown below. -----------------------------------------------------------

CLR via C # Deep solution note two-type design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.