O'Reilly .NET Framework Essentials, 2nd Edition第二章

CLR: manage and execute code written in .NET languages and is the basis of the .NET architecture.

2.1 CLR Environment  
How does the CLR work?      
CLR is the runtime engine that loads required classes, performs just-in-time complication on needed methods, enforces security checks, and accomplishes a bunch of other runtime functionalities.  
2.2 CLR Executables  .NET executables:
They carry not only code and data, but also metadata ----------VS-------  Typical windows executables: only code and data  The format of one .NET PE File 
  
 2.3 Metadata:
是机器可以识别的关于源文件的信息,或者可以翻译成“关于数据的数据”。 在.NET中,元数据包括:定义类型、版本信息、内部组件资料、以及其他标准化信息。 在.NET中,元数据就是在COM中的Type Library
元数据的作用
CLR CLR uses metadata for verification, security enforcement, cross-context marshaling, memory layout, and execution
Class Loader The Class Loader uses metadata to find and load .NET classes
JIT Complier JIT complier use metadata to compile Microsoft Intermediate Language(IL) code
Tools Tools use metadata to support integration.

2.3.3 Examine the metadata by a tool named ildasm.exe 2.3.3 Inspecting and Emitting Metadata       
System. Reflection Reflection API: Use those collectively classes to load and      insoect a .NET assembly to determine what types it supports.
System.Reflection.Emit
2.3.4 Interoperability Support
    Introduce 4 tools: the .NET assembly registration utility (RegAsm.exe); the type library exporter (tlbexp.exe); the type library importer (tlbimp.exe); the XML schema definition tool (xsd.exe)
RegAsm.exe To register a .NET assembly into the registry so COM clients can make use of it
tlbexp.exe Generate a type library file (.tlb) when you pass it a .NET assembly
tlbimp.exe Make a .NET assembly look like a COM component
xsd.exe 1 To convert an XML schema into a C# class, and vice versa. 2 can also take a .NET assembly and generate an XML schema definition (XSD) that represents the types within the .NET assembly
 
2.4 Assemblies and Manifests
       Assemblies: deployable units
       Manifests: the metadata that describes the assemblies
      Difference of the two definition: an assemble is a logic DLL or EXE, and a manifest is a detail description (metadata) of an assembly, including its version, what other assemblies it uses, and so on.
   2.4.1 Assemblies versus Components

       在COM时代, 微软的文档中经常出现 component , 有时指a COM Class , 有时指a COM module (DLL或EXE), 迫使读者或开发人员在遇见她的时候去根据上下文去理解是哪个意思. 在..NET中通过引入assembly这个新词来区别这个混淆.
       Assembly: a software component that supports plug-and-play, much like a hardware component. Theoretically, a .NET assembly is approximately equivalent to a COM module, In practice, an assembly can contain or refer to a number of types and physical files (including bitmap files, .NET PE files, and so forth) that are needed at runtime for successful execution. In addition to hosting IL code, an assembly is a basic unit of versioning, deployment, security management, side-by-side execution, sharing, and reuse.  
2.4.2 Unique Identities
        In COM: Given the number of GUID In COM, development and deployment can be tedious because you must use these magic numbers in your code and elsewhere all the time.
       In .NET: you refer to a specific type by its readable name and its namespace. Since a .NET readable name and its namespace are not enough to be globally unique, .NET guarantees uniqueness by using unique public/private key pairs. All assemblies that are shared by multiple applications must be built with a public/private key pair. Public/private key pairs are used in public-key cryptography. Since public-key cryptography uses asymmetric encryption, an assembly creator can sign an assembly with a private key, and anyone can verify that digital signature using the assembly creator’s public key.

 


  2.4.3 IL Code
  2.4.4 Versioning
           1 Static assemblies
              2 Dynamic assemblies
              3 Private assemblies
              4 Public or Shared assemblies
 2.4.5 Deployment: you have no longer to use registry to store activation and marshaling hints as in COM
  2.4.6 Security: an assembly itself has a code identity, which includes information such as the assembly’s shared name, version number, culture, and public key. Using this concept, the CLR cam verify whether an assembly is permitted to access system resources or make calls to other assemblies
 2.4.7 Side-by-Side Execution
              Side-by-side execution: the CLR allows any versions of the same, shared DLL to execute at the same time, on the same system, and even in the same oricess.
              MS .NET accomplishes side-by-side execution by using the versioning and deployment features that are innate to all shared assemblies. This concept allows you to install ant versions of the same, shared assembly on the same machine, without versioning conflicts or DLL Hell. The only caveat is that your assemblies must be public or shared assemblies, meaning that you must register them against the GAC using a tool such as the .NET Global Assembly Cache Utility. Once you have registered different versions of the same shared assembly into the AC, the human-readable name of the assembly no longer matters—what’s important is the information provided by .Net’s versioning and deployment features.
              Recall: the shared assembly’s version information + 8-byte hash of the shared assembly’s public key. Use the two information, the CLR can find the exact shared assembly that your application uses, and it will even verify that your 8-byte hash is indeed equivalent to that of the shared assembly.
  2.4.8 Sharing and Reuse  
       when you want to share your assembly with others, your assembly must have a shared or strong name, and you must register in the GAC.
  2.4.9 Manifests: Assembly Metadata
  an assembly manifests is metadata that describes everything about the assembly, including its identity, a list of files belonging to the assembly, references to external assemblies, exported types, exported resources, and permission requests. In short, it describes all the details that are required for component plug-and-play.

Use a class

In COM

In .NET

1 the COM library looks up in the registry to find the COM  component that exposes that class.
2 loads the component
3 tell the component to give it an instance of that class
4 return a reference to this instance

1 CLR peers right into the assembly manifest
2 determines which external assembly is needed
3 loads the exact assembly that’s required by your application
4 creates an instance of the target class

2.4.10 Creating Assemblies
you can create a single-module assembly or a multi-module assembly.
a single-module assembly: everything in a build is clumped into one exe or dll, which is easy created because a complier takes care of creating the single-module assembly for you.
a multi-module assembly, one that contains many modules and resource files, you have a few choices. One option is to use the Assembly Linker (al.exe). this tool takes one or more IL or resource files and spits out a file with an assembly manifest, also, we can use visual studio

2.4.11 Using Assemblies
To use an assembly, first import the assembly into your code, the syntax of which is dependent upon the language that you use. In c#, there is a simple
    using system;
when you build you application, you must tell the complier that you are referencing an external assembly.
2.5 Intermediate Language (IL)
Abstraction: we often use abstraction to hide the complexity of system or application services, providing instead a simple interface to the consumer. As long as we can keep the interface the same, we can change the hideous internals, and different consumers can use the same interface.
In language advances, scientists introduced different incarnations of language-abstraction layers, such as p-code and bytecode.
Produced by the Pascal-P complier, p-code is an intermediate language that supports procedural programming.
Generated by Java compliers, bytecode is an intermediate language that supports object-oriented programming. Bytecode is a language abstraction that allows Java code to run on different operating platforms, as long as the platforms have a Java Virtual Machine to execute bytecode
MS calls its own language-abstraction layer the Common Intermediate Language (CIL). Similar to bytecode, IL supports all object-oriented features, including data abstraction, inheritance, polymorphism, and useful concepts such as exceptions and events. In addition to these features, IL supports other concepts, such as properties, fields, and enumeration. Any .NET language may be converted into IL., so .NET supports multiple languages and perhaps multiple platforms in the future.

2.6 The CTS and CLS
Both the CTS and CLS ensure language compatibility, interoperability, and intefration.
2.6.1 CTS
all language should be equal in .NET. In order to make language integration a reality, MS has specified a common type system to which every .NET language must abide. MS supports a rich set of types, and there introduce some important, including value types, reference types, classes, interfaces, and delegates.
2.6.1.1 Value types: represent values allocated on the stack. They cannot be null and must always contain some data. When value types are passed into a function, they are passed by value, meaning that a copy of the values is made prior to function execution. This implies that the original value won’t change, no matter what happens to the copy during the function call. Since intrinsic types are small in size and don’t consume much memory, the resource cost of making a copy is negligible and outweighs the performance drawbacks of object management and garbage collection. Values types include primitives, structures, and enumerations; example:

int i;                            // primitives
struct point{int x, y}               // structures
enum State{off, on}             // enumerations


you can also create a value type by deriving a class from System.ValueType. One thing to note is that a value type is sealed; meaning that once you have derived a class from System.ValueType, no one else can derive from your class.

2.6.1.2 Reference types
if a type consumes significant memory resources, then a reference type provides more benefits over a value type. Reference types are so called because they contain references to heap-based objects and can be null. These types are passed by reference, meaning that when you pass such an object into a function, an address of or pointer to the object is passed—not a copy of the object, as in the case of a value type. Since you are passing a reference, the caller will see whatever the called function does to your object. The first benefit here is that a reference type can be used as an output parameter, but the second benefit is that you don’t waste extra resources because a copy is not made. If your object is large, then reference types are a better choice. In .NET, one drawback of a reference type is that it must be allocated on the managed heap, which means it requires more CPU cycles because it must be managed and garbage-collected by the CLR. In .NET, the closest concept to destruction is that finalization, but unlike destructors in C++, finalization is nondeterministic. In other words, you don’t know when finalization will happen because it occurs when the garbage collector executes. Since finalization is nondeterministic. Another drawback of reference types is that if reference-type objects hold on to expensive resources that will be released during finalization, system performances will degrade because the resources won’t released until these objects are garbage-collection. Reference types include classes, interfaces, arrays, and delegates. example:

如果一个类型消耗重要的内存资源,此时引用类型比值类型提供更多的好处。引用类型这样的叫它是因为他们包含很多基于堆栈的对象而且他们不能为空. 这些类型通过引用传输,这意味着当你传输这样的一个对象到一个函数的时候,这个对象的一个地址或指针被传输,而不是一个对象被复制,这与值类型一样。当你传输一个引用时,调用者将看到,无论这个调用是否针对你的对象。 这样做的第一个好处是引用类型可以作为对外输出的参数,但第二个好处是你不会浪费多余的资源因为并没有产生一个复制。 如果你的对象比较大,那么引用类型是您的一个很好的选择。 .NET中引用类型的一个缺点是它必须由堆栈来管理分配,这意味着它会要求更度的CPU循环因为它由CLR来管理和进行垃圾回收,在.NET中析构的最接近的意思是最终化,但和C++不同,最终化是不确定的,换句话说,你并不知道什么时候最终化发生,因为它是有垃圾回收开始开始执行的时候而发生。因此最终化是不确定的。另外一个缺点就是引用类型是如果一个引用类型对象在最终化的时候继续消耗即将释放的资源,系统性能将会下降因为这个资源不会被释放知道这些对象被垃圾回收站回收。引用类型包括类,接口,数组和代理。例: 

class car {}               //class
interface Istellting {}       //interface
int [] a = new int[5];        //array
delegate void process()      //delegate

2.6.1.3 Boxing and unboxing
MS .NET supports values types for performance reasons, but everything in .NET is ultimately an object. In fact, all primitive types have corresponding classes in the .NET Framework. For example, int is in fact an alias of System.Int32, and System.Int32 happens to derive form System.ValueType, meaning that it is a value type. Value types are allocated on the stack by default, but they can always be converted into heap-based reference-type object, called boxing. The following code snippet shows that we can create a box and copy the value of i into it:
基于性能的原因,MS .NET支持各种值类型,但是所有的东西最终都是对象。实际上,所有的私有类型都有一个想关联的类。举例来说,int实际上是System.Int32的另一种叫法,System.Int32是从System.ValueType中派生出来的。这意味着它是一个值类型。值类型是从堆栈中默认分配,但它们经常被转换为基于堆的引用类型对象,这就叫做装箱。
[说白了:就是将一个值类型转换成一个object类型!方法:声明一个值类型,再将这个值类型隐式的转换成一个object类型,并将这个复制给该object类型]
int i = 1;
Object box=i;            //隐式
Object box= Object (i);    //
显式

when you box a value, you get an object upon which you can invoke methods, properties, and events. For example, once you have converted the integer into an object, as shown in this code snippet, you can call methods that are defined in System.Object, including ToString(), Equals(), and so forth.
[
装箱还有一个方面:就是你一个把值类型转换成一个object类型,便可以调用object类型的方法,属性和事件。 


The reverse of boxing is of course unboxing, which means that you can convert a heap-based reference-type object into its alue-type equivalent, as shown here;
int j = (int)box; 
 

2.6.1.4 Class, Properties, indexers

原文地址:https://www.cnblogs.com/yang_sy/p/1087938.html