CLR via C# 第一章 CLR的执行模型

本文概述了将源代码编译为托管模块的过程,包括使用CLR、托管模块结构、程序集重用、CLR加载机制以及IL执行。重点讲解了编译器如何生成IL代码并由CLR编译为本地指令,以及程序集的部署优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1、将源代码编译成托管模块

1.CLR(Common Language Runtime)面向多种编程语言,提供内存管理、程序集加载、安全性、异常处理和线程安全等核心功能。

2.过程:源代码文件–> 编译器检查语法和分析源代码–>托管模块(managed module)

3.托管模块组成
(1) PE32或PE32+头——Windows PE文件头,其中PE32+只能在WIN64上运行。
标识了文件类型(GUI、CUI、DLL)
包含一个时间标记(文件的生成时间)
如果是包含本机(native)CPU代码的模块,这个头含与本机CPU代码有关的信息

(2) CLR头
包含要求的CLR版本
一些flag标志
托管模块Main方法的MethodDef元数据token
模块的元数据、资源、强名称、一些标志以及一些不重要的数据项的位置/大小

(3) 元数据——数据表的集合
源代码中定义的类型和成员的元数据表、以及引用的类型元数据表

(4) IL代码——编译器编译源代码生成的代码
在运行时,CLR(需要安装.NET Framework)将IL编译成本机CPU指令

特殊:Microsoft的C++编译器默认生成包含 native 的exe/dll 文件,可以通过指定/CLR命令行开关生成包含托管代码的模块。

1.2、将托管模块合并成程序集

通过AL.exe链接
Assembly:重用、安全性、版本控制的最小单元。程序集是自描述的,在程序集的模块中包含了引用的程序集的相关信息(包括版本号),不需要在注册表或Active Directory Domain Services中保存额外信息。所以与非托管组件代码相比,程序集更容易部署。

1.3、加载公共语言运行时

检查系统目录中是否存在 mscoree.dll 以确定安装了.NET Framework:
在这里插入图片描述
Windows通过检查exe文件头,决定创建32位进程还是64位进程,随后在进程地址空间加载相应版本的MSCorEE.dll,然后进程的主线程调用MSCorEE.dll中定义的一个方法初始化CLR,加载EXE程序集,最后调用其入口方法(Main),托管程序启动并运行。

如果是非托管引用程序调用 LoadLibrary 加载托管程序集,Windows会自动加载并初始化CLR以处理程序集中的代码。这个时候进程已经启动并运行了,可能会限制程序集的可用性。

1.4、执行程序集的代码

托管程序集包含元数据和 IL,IL是面向对象的机器语言。Microsoft提供了IL汇编器:ILAsm.exe
和IL反汇编器:ILDasm.exe

CLR是如何执行程序集的代码的?
在Main方法执行之前,CLR会检测出Main的代码引用的所有类型。这导致CLR分配一个内部数据结构来管理对引用类型的访问。
在这里插入图片描述
如图,Main方法引用了 Console 类,导致CLR分配一个内部结构。在这个数据结构中,Console类定义的每个方法都有一个对应的entry,每个entry 都含有一个地址,根据此地址可以找到方法的实现。对这个结构初始化时,CLR将每个 entry 设置成 指向包含在CLR内部的一个未编档函数(称为JITCompliler)
Main函数首次调用WriteLine时,JITCompiler函数会被调用,此时,JIT知道它要定义的是哪个类型定义的哪个方法。然后,JIT会在定义该类型的程序集的元数据中查找被调用方法的IL。通过验证(verification)后,JIT将方法的IL代码编译成本机CPU指令(x86、x64、ARM等)并保存到动态分配的内存块中。然后,JIT回到CLR为类型创建的内部数据结构中,修改entry 对JITCompliler的引用,使其指向内存块。最后,JIT跳转执行内存块中的代码,执行完毕后回到Main函数,继续执行Main函数。第二次执行该方法时则会跳过JIT直接执行本机代码。

注意:CLR的JIT编译器会对本机代码进行优化,受 /optimize和 /debug 两个C#编译器开关影响。

CLR via C# 第4版 英文PDFKristin, words cannot express how /feel about our life together. cherish our family and all our adventures. I'm filled each day with love for Aidan (age 9)and Grant (age 5), you both have been an inspira- tion to me and have taught me to play and have fun Watching the two of you grow up has been so rewarding and enjoyable for me. am lucky to be able to partake in your lives. love and ap preciate you more than you could ever know Contents at a glance Introduction PART I CLR BASICS CHAPTER 1 The clr's execution model CHAPTER 2 Building, Packaging, Deploying, and Administering Applications and Types 33 chaPTeR 3 Shared Assemblies and Strongly Named Assemblies 65 PART I DESIGNING TYPES CHAPTER 4 Type Fundamentals 91 CHAPTER 5 Primitive, Reference, and Value Types 111 CHAPTER 6 Type and Member Basics 151 CHAPTER 7 Constants and fields 175 chaPTer 8 Methods 181 chaPTer 9 Parameters 209 CHAPTER 10 Properties 227 CHAPTER 11 Events 249 CHAPTER 12 Generics 265 CHAPTER 13 Interfaces 295 PARTⅢ ESSENTIAL TYPES CHAPTER 14 Chars, Strings, and Working with Text 317 CHAPTER 15 Enumerated Types and Bit Flags 361 CHAPTER 16 Arrays 373 CHAPTER 17 Delegates 391 CHAPTER 18 Custom Attributes 421 CHAPTER 19 Nullable value Types 441 PART IV CORE FACILITIES CHAPTER 20 Exceptions and state management 451 CHAPTER 21 The Managed Heap and Garbage Collection 505 CHAPTER 22 CLR Hosting and AppDomains 553 CHAPTER 23 Assembly Loading and reflection 583 CHAPTER 24 Runtime serialization 611 CHAPTER 25 Interoperating with WinRT Components 643 PAR V THREADING ChaPTEr 26 Thread basics 669 CHAPTER 27 Compute-Bound Asynchronous Operations 691 CHAPTER 28 IyO-Bound Asynchronous Operations 727 CHAPTER 29 Primitive thread Synchronization Constructs 757 CHAPTER 30 Hybrid Thread Synchronization Constructs 789 Index 823 Contents at a glance Contents Introduction XX PART CLR BASICS Chapter 1 The Clrs Execution Model 3 Compiling Source Code into Managed Modules Combining managed modules into assemblies Loading the Common Language Runtime 8 Executing Your Assembly's Code 11 IL and∨ erification 16 Unsafe Code The Native Code generator tool: ngen. exe 19 The Framework Class Library 22 The Common Type System The Common Language Specification Interoperability with Unmanaged Code 30 Chapter 2 Building, Packaging, Deploying, and Administering Applications and Types 33 NET Framework Deployment Goals 34 Building Types into a Module 35 Response Fil 36 A Brief Look at metadata 38 What do you think of this book We want to hear from you Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit microsoft. com/learning/booksurvey Combining Modules to Form an Assembly 45 Adding Assemblies to a Project by Using the Visual Studio IDE.51 Using the assembly Linker Adding Resource Files to an Assembly 53 Assembly Version Resource Information .54 Version numbers ..58 Culture Simple Application Deployment(Privately deployed Assemblies)...60 Simple Administrative Control(Configuration) 62 Chapter 3 Shared Assemblies and Strongly Named Assemblies 65 Two Kinds of Assemblies, Two Kinds of Deployment 66 Giving an Assembly a Strong Name 67 The global Assembly Cache 72 Building an Assembly That References a Strongly Named Assembly..74 Strongly named assemblies are tamper-Resistant 75 Delayed Signing Privately Deploying Strongly Named Assemblies How the Runtime Resolves Type References 80 Advanced Administrative Control( Configuration) 83 Publisher Policy control 86 PART I DESIGNING TYPES Chapter 4 Type Fundamentals 91 All Types Are Derived from System Object .91 Casting Between Types 93 Casting with the C# is and as Operators Namespaces and assemblies 97 How Things relate at Run time .101 Chapter 5 Primitive, Reference, and Value Types 111 Programming Language Primitive Types 111 Checked and Unchecked Primitive Type Operations 115 Reference Types and value Types 118 Boxing and Unboxing Value Types 124 Changing Fields in a Boxed Value Type by Using Interfaces and Why You Shouldnt Do This) 136 Object Equality and Identity 139 Object hash Codes .142 The dynamic Primitive Type ......144 Chapter 6 Type and member Basics 151 The Different Kinds of Type Members .151 Type visibilit 154 Friend assemblies 154 Member accessibility .156 Static Classes ...158 Partial Classes, Structures, and Interfaces .159 Components, Polymorphism, and Versioning 160 How the CLR Calls Virtual Methods, Properties, and Events 162 Using Type Visibility and Member Accessibility Intelligently...166 Dealing with Virtual Methods When Versioning Types 16 Chapter 7 Constants and Fields 175 Constants 175 Fⅰe|ds ...177 Chapter 8 Methods 181 Instance Constructors and Classes(Reference Types) 181 Instance Constructors and Structures(Value Types) 184 Type Constructors 187 Contents x Operator Overload Methods 191 Operators and Programming Language Interoperability 193 Conversion Operator Methods 195 Extension method 198 Rules and guidelines ....,200 Extending Various Types with Extension Methods 201 The Extension Attribute 203 Partial Methods 204 Rules and guidelines 207 Chapter 9 Parameters 209 Optional and Named Parameters 209 Rules and guidelines 210 The defaultParameter value and optional Attributes 212 Implicitly Typed Local Variabl 212 Passing parameters by reference to a Method 214 Passing a variable Number of arguments to a Method 220 Parameter and Return Type Guidelines 223 Const-nes 224 Chapter 10 Properties 227 Parameterless Properties 227 Automatically Implemented Properties 231 Defining Properties Intelligently 232 Object and collection Initializers 235 Anonymous Type .237 The System. Tuple type 240 Parameterful Properties 242 The performance of calling property accessor Methods 247 Property Accessor Accessibility 248 Generic prop A roperty Access 248
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值