《CLR via C#》笔记:第1部分 CLR基础

  • 本博客所总结书籍为《CLR via C#(第4版)》清华大学出版社,2021年11月第11次印刷(如果是旧版书籍或者pdf可能会出现书页对不上的情况)
  • 你可以理解为本博客为该书的精简子集,给正在学习中的人提供一个“glance”,以及对于部分专业术语或知识点给出解释/博客链接。
  • 【本博客有如下定义“Px x”,第一个代表书中的页数,第二个代表大致内容从本页第几段开始。(如果有last+x代表倒数第几段,last代表最后一段)】
  • 电子书可以在博客首页的文档-资源归档中找到,或者点击:传送门自行查找。如有能力请支持正版。(很推荐放在竖屏上阅读本电子书,这多是一件美事)
  • 作为导引,你可能需要了解以下的知识
    Mono,CLR,.net,Net Framework之间的关系:传送门
    CLR官方手册:传送门(英文警告)
    什么是CLR:传送门
    Unity的基本代码编写及其使用。
    鲁棒性:传送门
    公钥,私钥,数据签名:传送门
  • 同时建议实践与理论并行,可以找些教程边写边学。
  • 个人博客网址:《CLR via C#》笔记:第1部分 CLR基础 - Sugar的博客,如文章转载中出现例如传送门失效等纰漏,建议直接访问原博客网址。
  • 欢迎加群学习交流:637959304 进群密码:(CSGO的拆包密码) 

目录

第一章 CLR的执行模型

第二章 生成、打包、部署和管理应用程序及类型

第三章 共享程序集和强命名程序集


该部分主要了解一下CLR模型即可

第一章 CLR的执行模型

  • CLR:Common Language Runtime,可由多重编程语言使用的“运行时(即Runtime)”。
    核心功能:内存管理,程序集加载,安全性,异常处理,线程同步。(P3 last)

  • 除了生成IL,编译器要在每个托管模块中生成完整的元数据(metadata),一个数据表集合。元数据有多重用途:1、表面编译时对原生C/C++头和库文件的需求。2、VStudio用元数据帮助程序员写代码。3、CLR代码验证过程使用元数据确保代码只执行“类型安全”的操作 4、元数据允许将对象的字段序列化到内存块,发送给另一台机器然后反序列化。 5、允许垃圾回收期跟踪对象生存期。(P5 last)
  • 程序集(assembly):首先,程序集是一个或多个模块/资源文件的逻辑性分组。其次,程序集是重用、安全性以及版本控制的最小单元。取决于你选择的编译器或工具,既可生成单文件程序集,也可生成多文件程序集。在CLR的世界中,程序集相当于“组件”。(P6 last3)

  • CLR的执行:(P11 last)
    1、下图中要执行main函数,CLR检测出main中代码引用所有类型,并分配一个内部数据结构管理引用类型访问
    2、Console类型定义的每个方法都有一个记录项,记录项都含有一个地址,根据此地址即可找到方法的实现。
    3、对这个结构初始化时,CLR将每个记录项都设置成包含在CLR内部的一个未编档函数,该函数称为JITCompiler。
  • main首次调用writeline,JITCompiler函数被调用,负责将IL代码编译成本机CPU指令。
    当第二次调用writeline会跳过函数直接执行内存块中代码。

  • Visual Studio:“调试”(Debug)配置指定/optimize-和ldebug:full开关,而“发布”(Release)配置指定/optimize+和/debug:pdbonly开关(P13 4)

  • IL:1、基于栈 2、指令无类型(typeless)
    IL优势:对底层CPU抽象,具有应用程序的鲁棒性(健壮性)和安全性(P15 3)
  • 通过验证托管代码,可确保代码不会不正确地访问内存,不会干扰到另一个应用程序的代码。这样就可以放心地将多个托管应用程序放到同一个 Windows虚拟地址空间运行。用一个进程运行多个应用程序,可减少进程数,从而增强性能,减少所需的资源,健壮性也没有丝毫下降。这是托管代码相较于非托管代码的另一个优势。

  • Framework类库(Framwork Class Library,FCL):是一组程序集的统称,包含数千个类型定义。
    利用FCL创建应用程序举例:Web服务,基于HTML的Web窗体/MVC应用程序(网站),“富”Windows GUI应用程序,Windows控制台应用程序,Windows服务,数据库存储过程,组件库。(P20 1)
  • System命名空间(P21 3)

  • 通用类型系统(Common Type System,CTS):类型是CLR的根本,CTS为微软指定的规范类描述类型的定义和行为:(P22 1)
  • 字段(Field):作为对象状态一部分的数据变量。字段根据名称和类型来区分。
  • 方法(Method):针对对象执行操作的函数,通常会改变对象状态。方法有一个名称、一个签名以及一个或多个修饰符。签名指定参数数量(及其顺序);参数类型;方法是否有返回值;如果有返回值,还要指定返回值类型。
  • 属性(Property):对于调用者,属性看起来像是字段。但对于类型的实现者,属性看起来像是一个方法(或者两个方法R)。属性允许在访问值之前校验输入参数和对象状态,以及/或者仅在必要时才计算某个值。属性还允许类型的用户采用简化的语法。最后,属性允许创建只读或只写的“字段”。
  • 事件(Event):事件在对象以及其他相关对象之间实现了通知机制。例如,利用按钮提供的一个事件,可在按钮被单击之后通知其他对象。
  • CTS还指定了类型可见性规则以及类型成员的访问规则。(P23 2)
  • private:成员只能由同一个类(class)类型中的其他成员访问。
  • family:成员可由派生类型访问,不管那些类型是否在同一个程序集中。注意,许多语言(比如C++和C#)都用protected修饰符来标识family。
  • family and assembly:成员可由派生类型访问,但这些派生类型必须在同一个程序集中定义。许多语言(比如C#和Visual Basic)都没有提供这种访问控制。当然,IL汇编语言不在此列。
  • assembly:成员可由同一个程序集中的任何代码访问。许多语言都用internal修饰符来标识assembly。
  • family or assembly:成员可由任何程序集中的派生类型访问。成员也可由同一个程序集中的任何类型访问。C#用protected internal修饰符标识family or assembly。
  • public:成员可由任何程序集中的任何代码访问。
  • 公共语言规范(Common Langusge Specification,CLS):详细定义了一个最小功能机,以进行不同语言的集成。在跨语言编程时需要遵守CLS规范。
  • CLR支持三种托管代码与非托管代码的互相操作情形(以满足不重新设计现有代码的需求):1、托管代码能调用DLL中的非托管函数 2、托管代码可以使用现有的COM组件(服务器) 3、非托管代码可以使用托管类型(服务器)

该部分了解一下如何打包部署,公钥私钥的概念等即可

第二章 生成、打包、部署和管理应用程序及类型

  • .NET Framework正在尝试彻底解决DLL hell问题,解决应用程序状态在用户硬盘四处分散的问题。
    同时.NET Framework利用代码访问安全性(Code Access Security)让用户决定授予应用的信任权限。(P30 last)
  • 如何将包含多个类型的源代码文件转变为可以部署的文件:利用指令csc.exe Program.cs生成EXE文件(P31 1)
  • 元数据概述:探讨上述生成的Program.exe具体生成了什么。托管PE文件由4部分组成:PE32(+)头、CLR头、元数据以及IL。(P33 last2)Progam是公共密封类,从System.Object派生,还定义了两个方法:Main和.ctor(构造器),Main是公共静态方法,用IL代码实现,构造器是公共方法,也用IL实现。(P37 last)
    元数据是由几个表构成的二进制块:定义表(definition table)、引用表(reference table)、清单表(mainifest table)。ILDasm会处理元数据表。(P34 last)

  • 将模块合并成程序集:Program.exe是程序集不是含有元数据的PE文件。程序集是一个或多个类型定义文件及资源文件的集合。
    CLR操作程序集,会首先加载包含“清单”元数据表的文件,在根据“清单”来获取程序集中的其他文件名称。程序集特点:1、定义了可重用的类型 2、用一个版本号标记 3、可以关联安全信息。(P39 1)

  • 使用VStudio将程序集添加到项目中(P44 last),使用程序集连接器(P45 3),为程序集添加资源文件(P46 last2)程序集版本信息(P47 last)
  • 版本号:下图所示为2.5.719.2。2.5即公众看到的版本号,719是程序集的build号,2指出当前build的修订次数。如果公司每天都生成程序集,那么应每天递增build号。
    程序集三个版本号:AssemblyFileVersion、AssemblyInformationVersion、AssemblyVersion(P50 3)

  • 语言文化(P51 2)
  • 简单应用程序部署(私有部署的程序集)(P52 2),简单管理控制(P53 last3)

该部分看看图即可

第三章 共享程序集和强命名程序集

  • 本章节重点:如何创建可由多个应用程序共享的程序集。
  • CLR支持两种程序集:弱命名程序集(weakly named assembly)和强命名程序集(strongly named assembly)。两者程序集结构完全相同,生成工具也相同。
    区别:强命名程序集使用发布者的公钥/私钥进行了签名。(P58 4)弱命名只能以私有方式部署,强命名可以私有也可以全局。
  • 为程序集分配强名称(P59 2),公钥长度太大,为了简化工作,设计使用了公钥标记(public key token),该标记为公钥的64位哈希值。文件签名的准确含义:生成强命名程序集时,程序集的FileDef清单元数据表列出构成程序集的所有文件。每将一个文件名添加到清单,都对文件内容进行哈希处理。(P61 2)

  • 全局程序集缓存(Global Assembly Cache,GAC):由多个应用程序访问的程序集必须放到公认的目录,该目录位置即全局程序集缓存。(P63 3)
  • 在生成的程序集中引用强命名程序集(P65 3)
  • 强命名程序集能防篡改(P66 3)
  • 延迟签名(P67 1)
  • 私有部署强命名程序集(P69 1)
  • “运行时”如何解析类型引用:1、相同文件-早期绑定 2、不同文件,相同程序集-当前程序集元数据的FileDef表 3、不同文件、不同程序集-加载被引用程序集的清单文件(P70 1)

  • 高级管理控制:演示如何将被引用程序集的文件移动到应用程序基目录下的一个子目录,以及CLR如何通过应用程序的XML配置文件来定位发生移动的文件。(P73 last)发布者策略控制(P75 last2)
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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值