CLR via C # Note 2,

Source: Internet
Author: User
Tags mscorlib

CLR via C # Note 2,
Type BasicsAll types are derived from System. Object CLR. All objects must be created using the new operator. Employee e = new Employee ("Constructor Parameters"); the following isNew operatorWhat you 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 stack also requires some additional (overhead member) members, namely, "type object pointer" and "sync block index ). These members are used by CLR to manage objects. The number of bytes of these additional members is the size of the incoming object. #2. It allocates the number of bytes required by the specified type from the managed heap to allocate the object memory. All allocated bytes are set to 0. #3. It initializes the "type Object Pointer" and "synchronization block Index" members of the object. #4. Call the instance constructor to pass in any real parameters specified in the new call. Most compilers automatically generate code in the constructor to call a base class constructor. Each type of constructor is responsible for initializing the instance fields defined by this type. The most common call is the System. Object constructor. This constructor simply returns and does not do anything else. After all new operations are performed, a reference (or pointer) pointing to the new object is returned ). In the previous sample code, this reference is saved to variable e, which has the Employee type.Class "new instance" and "instance Member": Two different "instances ". One is a class instance, that is, a specific object. The other is the instance field defined in the class. The so-called "instance field" refers to non-static fields, sometimes referred to as "instance members ". Simply put, instance members belong to class objects, while static members belong to Classes.Type SecurityCLR always knows the type of an object (an instance of a certain type. All expressions are parsed into instances of a certain type. In the code generated by the compiler, only the operations that are valid for this type are executed. Compared with non-type security languages, the advantage of type security languages is that many errors made by programmers can be detected during compilation, make sure the code is correct before you try to execute it. In addition, the language can usually generate smaller and faster code during compilation, because they can make more assumptions during compilation and implement those assumptions in the generated IL and metadata.Type conversionOne of the most important features of CLR is type security. During runtime, CLR always knows the type of an object. When you call the GetType () method, you always know the exact type of an object. During development, developers often convert an object from one type to another. CLR allows an object to be converted to its (actual) type or any base type. #1: conversion to the base type is considered a safe implicit conversion. #2: when converting an object to a derived type, C # requires only explicit conversion, because such conversion may fail at runtime. During runtime, CLR checks the type operation to determine whether it is always converted to the actual type of the object or any of its base types. This is the design of type security. If CLR permits such a transformation, there will be no type of security, and unexpected results will occur-including application crashes, and the emergence of Security Vulnerabilities (because one type can be easily disguised as another type ). Type camouflage is the root cause of many security vulnerabilities. It also damages the stability and robustness of applications. Type security is an important objective of CLR. Is operator, checks whether an object is compatible with the specified type, and returns a Boolean value; true or false. Note that the is operator will never throw an exception. If the object reference is null, the is Operator always returns false. Object o = new Object (); if (o is Employee) {Employee e = (Employee) o;} in this Code, CLR checks the Object type twice. CLR type check enhances security, but it will undoubtedly affect performance. The CLR must first determine the actual type of the object referenced by the variable. Then, CLR must traverse the inheritance hierarchy and use each base type to check the specified type (such as Employee ). The above is a very common programming mode, so C # provides the as operator specifically to simplify the writing of this code and improve performance. Employee e = o as Employee; if (e! = Null ){//.....} the as operator works in the same way as forced type conversion, but it never throws an exception-on the contrary, if the object cannot be transformed, the result is null. Therefore, the correct method is to check whether the final generated reference is null. Do not directly use the final reference; otherwise, a System. NullReferenceException may be thrown. Note: C # Allows defining the conversion Operator Method in a type. These methods are called only when a transformation expression is used. They are never called when the as or is Operator of C # is used.Namespace)It is 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 for string processing. The using directive instructs the compiler to append different prefixes to each type until a match is found. The use of using not only greatly reduces the amount of typing, but also helps enhance the readability of the Code. The using command can also be used to create an alias for a type or namespace. If you only want to use a namespace of a few types, and do not want all its types to run out of the "contaminated" global namespace, the alias is very convenient. Using System; using jack = CSI. Widget;Important: CLR does not know anything about namespaces. When accessing a type, the CLR needs to wait until the complete name of the type (which may be a long name containing a period) and the specific set of programs where the type definition is located. In this way, the "RunTime" can load the correct assembly, locate the target type, and operate on it. The compiler scans all referenced assemblies to find the definition of the type. Once the correct assembly is found, the assembly information and type information will be embedded into the metadata of the finally generated managed module. To obtain assembly information, you must pass the Assembly that defines the "referenced type" to the compiler. By default, the C # compiler automatically searches for the "referenced type" in the MSCorLib. dll program, even if you do not explicitly tell it to do so. MSCorLib. dll contains the definition of all core Framework class libraries (FCL), such as objects, Int32, and String. Namespaces and assembly (implementing a type of file) are not necessarily related. In particular, various types in the same namespace may be implemented in different programming sets. In a program set, it may also contain types in different namespaces.Runtime relationshipsThe relationships between types, objects, thread stacks, and managed stacks during runtime. Differences between calling static methods, instance methods, and virtual methods. A Microsoft Windows Process with CLR loaded. There may be multiple threads in this process. When a thread is created, it is allocated to a 1 MB stack. The stack space is used to pass real parameters to the method and to call local variables defined inside the method. Stack is built from high memory address to low memory address.Stack frameStack frame refers to a method call in the call stack of the current thread. Each method call made during the execution thread is created in the call stack and pushed into a stack frame. So far, we have discussed the relationship between the source code, IL, and JIT compiled code, it also discusses the thread stack, real parameters, local variables, and how these real parameters and variables reference objects hosted on the stack. I understand that the object contains a pointer pointing to the object type object (the type object contains static fields and table ). The JIT compiler also discusses how to determine the call methods of static methods, non-real-world examples, and real-world examples. All these understandings can help you deeply understand the way CLR works.Note that both the Employee and Manager objects contain "type Object Pointer" members. This is because the type object is essentially an object.When creating a type object, CLR must initialize these members. What is initialization? When CLR starts running in a process, it immediately creates a special Type object for the System. Type defined in MSCorLib. dll. Employee and ManagerType objectAre all "instances" of this type ". Therefore, their Type object pointer members are initialized to reference System. Type objects, as shown below. -----------------------------------------------------------

Related Article

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.