本文是对几个与.NET框架相关的缩写的总结
1.CIL: Common Intermediate Language - 公共中间语言
CIL也叫MSIL(Microsoft Intermediate Language),后来进行了标准化,成为CIL,有时也简写作IL。是一种高级程序语言。C#、VB.NET程序经过各自的编译器生成CIL代码。CIL需要.NET运行时(.NET runtime)环境的支持,执行前,进行一个被称为JIT(Just-in-time)的二次编译过程,才能变成计算机可以识别的指令。
2.ILDASM: Intermediate Language DisASseMbler - 中间语言反汇编工具
ILDASM可以将.NET程序反编译为CIL,ILASM可以将CIL编译成可执行文件或类库
在Windows的CMD(命令提示符,控制台)中并没有ildasm命令和ilasm命令,要想在CMD中添加这个命令,需要在里面执行一个BAT脚本,这个脚本就在VS安装目录下的“\Common7\Tools\”里,名叫“VsDevCmd.bat”。在我的计算机里,这个BAT文件的绝对路径为:“D:\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat”
如果不想这么麻烦,可以在开始菜单中所有程序里的VS目录(我的是“Microsoft Visual Studio 2012”)下,找到“Visual Studio Tools”里的“VS2012 开发人员命令提示”,进入后就可以直接使用ildasm命令了。事实上,这是一个CMD的快捷方式,新建个快捷方式,修改下面两项也能达到“VS2012 开发人员命令提示”同样的效果:
目标:%comspec% /k ""D:\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat""
起始位置:"D:\Microsoft Visual Studio 11.0\"
在这个命令行中输入命令ildasm可以打开一个应用程序“IL DASM”,用这个程序打开.NET生成的EXE文件,即可实现.NET应用程序IL反编译。下面是我用几个.NET上的语言建立的类库通过ILDASM反编译的结果:
1)Visual C# 控制台应用程序:TestA
namespace TestA
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
生成的可执行文件为TestA.exe(大小:5,120字节,占用空间:8,192字节),用IL DASM查看后,结构如下:
双击“Main : void(string[])”后可以看到下面这段代码:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello World!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method Program::Main
2)Visual Basic 控制台应用程序:TestB
Module Module1
Sub Main()
Console.WriteLine("Hello World!")
End Sub
End Module
生成的可执行文件为TestB.exe(大小:24,576字节,占用空间:24,576字节),用IL DASM查看后,结构如下:
双击“Main : void()”后可以看到下面这段代码:(与C#的非常像)
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello World!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method Module1::Main
3)F# 控制台应用程序:TestC
[<EntryPoint>]
let main argv =
printfn "Hello World!"
0
生成的可执行文件为TestC.exe(大小:4,608字节,占用空间:8,192字节),用IL DASM查看后,结构如下:
双击“main : int32(string[])”后可以看到下面这段代码:(和C#与VB.NET有很大不同)
.method public static int32 main(string[] argv) cil managed
{
.entrypoint
.custom instance void [FSharp.Core]Microsoft.FSharp.Core.EntryPointAttribute::.ctor() = ( 01 00 00 00 )
// 代码大小 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello World!"
IL_0006: newobj instance void class
[FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class
[FSharp.Core]Microsoft.FSharp.Core.Unit,class
[mscorlib]System.IO.TextWriter,class
[FSharp.Core]Microsoft.FSharp.Core.Unit,class
[FSharp.Core]Microsoft.FSharp.Core.Unit,class
[FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
IL_000b: call !!0
[FSharp.Core]Microsoft.FSharp.Core.ExtraTopLevelOperators::PrintFormatLine<class
[FSharp.Core]Microsoft.FSharp.Core.Unit>(class
[FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class
[mscorlib]System.IO.TextWriter,class
[FSharp.Core]Microsoft.FSharp.Core.Unit,class
[FSharp.Core]Microsoft.FSharp.Core.Unit>)
IL_0010: pop
IL_0011: ldc.i4.0
IL_0012: ret
} // end of method Program::main
4)Visual C++ 控制台应用程序:TestD
建立一个空项目,里面添加一个代码文件TestD.cpp,写入下面代码:
#include<stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
生成的可执行文件为TestD.exe(大小:31,232字节,占用空间:32,768字节),用IL DASM查看:
这个文件没有有效的CLR头,因此无法进行反汇编
3.ILASM: Intermediate Language ASseMbler - 中间语言编译工具
把上面2.1中的C#程序,在ILDASM中打开后转储到一个扩展名为il的程序,指定名称:xxx.il
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly TestA
{
.custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 1A 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B // ....NETFramework
2C 56 65 72 73 69 6F 6E 3D 76 34 2E 35 01 00 54 // ,Version=v4.5..T
0E 14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C // ..FrameworkDispl
61 79 4E 61 6D 65 12 2E 4E 45 54 20 46 72 61 6D // ayName..NET Fram
65 77 6F 72 6B 20 34 2E 35 ) // ework 4.5
.custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 05 54 65 73 74 41 00 00 ) // ...TestA..
.custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 05 54 65 73 74 41 00 00 ) // ...TestA..
.custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20 // ...Copyright ..
20 32 30 31 35 00 00 ) // 2015..
.custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 66 36 39 37 61 37 32 32 2D 64 35 64 34 // ..$f697a722-d5d4
2D 34 36 62 36 2D 39 39 33 34 2D 35 30 33 62 62 // -46b6-9934-503bb
31 64 64 37 30 36 61 00 00 ) // 1dd706a..
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 ) // ...1.0.0.0..
// --- 下列自定义特性会自动添加,不要取消注释 -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004
.ver 1:0:0:0
}
.module TestA.exe
// MVID: {243BEAC1-C1FD-4F5D-91A9-4381400D3AEE}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00020003 // ILONLY 32BITPREFERRED
// Image base: 0x01260000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit TestA.Program
extends [mscorlib]System.Object
{
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello World!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctor
} // end of class TestA.Program
// =============================================================
// *********** 反汇编完成 ***********************
// 警告: 创建了 Win32 资源文件 C:\Users\Tsybius\Desktop\xxx.res
除了xxx.il外,转储过程还会创建一个文件:“xxx.res”,不用管它。在命令行中用ilasm命令可以编译xxx.il了
编译后生成了一个可执行文件xxx.exe,在命令行中运行这个程序,还是可以打印出字符串“Hello World!”:
4.CTS: Common Type System - 公共类型系统
面向.NET的语言(如C#、VB.NET、Delphi.NET等)需要满足的规则。CTS规定了可以在语言中定义的类型(如类、结构、委托等),规定了各种访问性(如Private、Public、Family(C#中的Protected)、Assembly(C#中的internal)、FamilyAndAssembly(C#中没有)和FamilyOrAssembly(C#中的protected internal)),规定了一些约束(如所有的类都隐式继承自System.Object、所有的类只能继承自一个基类)。CIL实现了CTS的全部功能,C#等语言实现的功能是CIL的子集。
5.CLS: Common Language Specification - 公共语言规范
CLS是CTS的一个子集,包括几种面向对象的编程语言的通用功能。符合CLS的组件和工具能够保证与其他符合CLS的组件和工具交互操作
6.CLR: Common Language Runtime - 公共语言运行时
也称为.NET运行时(.NET runtime)。CLR是一个软件层或代理,它管理了.NET程序集的执行,主要包括:管理应用程序域、加载和运行程序集、安全检查、将CIL代码即时编译为机器代码、异常处理、对象析构、垃圾回收等
.NET程序集包括以下几部分:(托管可执行文件的通用结构)
1)PE/COFF头:包含了供操作系统查看和利用的信息
2)CLR头:最重要的作用之一就是告诉操作系统这个PE/COFF文件是一个.NET程序集,区别于其他类型可执行程序
3)CLR数据:元数据、IL代码、托管的结构化异常处理信息、托管资源
4)本地数据和代码(如果有)
7.CLI: Common Language Infrastructure - 公共语言基础
CLI是一个由ECMA和ISO进行标准化的国际标准。CLI包括:CIL、CTS、CLS、VES、元数据、基础框架。CLI是.NET框架是该标准的具体实现。CLR也是.NET框架中对VES的具体实现,VES即“Virtual Execution System”,汉语意为虚拟执行系统
Standard ECMA-335 - Common Language Infrastructure (CLI)
http://www.ecma-international.org/publications/standards/Ecma-335.htm
8.BCL: Base Class Library - 基础类库
BCL中包含了与编译器及CIL语言关系紧密的核心类型,以及常见开发任务中都会使用到的类型。BCL是FCL的一个子集
9.FCL: Framework Class Library - 框架类库
FCL是一个.NET Framework上的标准库的实现,它在CLI中定义。FCL是一组可复用的类、接口和值类型,BCL是FCL的核心并为之提供了最基本的功能
FCL包括下面几个库:
1)Base Class Library
2)Runtime Infrastructure Library
3)Network Library
4)Reflection Library
5)XML Library
6)Extended Array Library
7)Extended Numerics Library
8)Parrallel Library
9)Vararg Library
END