Preface:
1、应用程序一般都包含好几个类型(type,在一些参考资料中,也称为组件component)。
2、通常情况下,应用程序既包含我们自己创建的类型,也包括微软和其他一些组织创建的类型。如果这些类型都采用支持CLR的语言开发,那么它们将可以无缝地在一起协作执行。我们甚至可以用一种语言开发一个基类,然后用另一种语言开发它的子类。
2.1 .NET框架部署目标
Windows的名声一直不好,其主要原因有三点:
1、应用程序会使用一些来自微软和其他厂商的动态链接库(dll)。由此而产生的“DLL hell“的问题。
2、复杂的安装过程。目前大多数应用程序的安装都会影响到系统的各个部分。例如安装一个应用程序会导致文件被复制到许多目录中,注册表要更新,快捷链接也会安装到桌面、开始菜单和快速启动工具栏中。这样直接导致应用程序不能被隔离为一个单独的实体。
3、与安全有关。安装应用程序时的所有操作对用户并非是完全透明的,因此有安全隐患。
.NET框架对应用程序的部署,改良甚至修正了上述问题:DllHell基本解决,代码访问安全机制模型(Code Access Security)更是在程序的安全方面迈出了一大步,我们可以自己决定程序集的安全性,Windows用户可以对安装程序具有更多的控制。
2.2将类型生成为模块
(MSCorLib.dll是一个特殊的文件,它包含了.NET框架中所有的核心类型,如字节、整数、字符、字符串等。由于这些类型的使用非常频繁,所以在命令行下编译C#程序时,C#编译器会自动引用该程序集)
我们可以通过编译器比如csc.exe来将类型生成这些模块.
(1)CUI(Control User Interface控制台界面应用程序);比如:csc.exe App.cs
(2)GUI(Graph User Interface图形界面应用程序);比如:csc /target:winexe App.cs
(3)Dll(应用程序集,和以前概念的Dll--动态连接库应该有所区分);比如: csc /target:library App.cs
(4)netModule(单个的托管模块,这些东西非简单集合组成Dll,更有助于我们管理类型);比如:csc /target:module App.cs
通过csc生成的Exe文件是一个标准的(托管)PE文件,可以运行于32,64位的Windows平台上。
托管PE包括
1、PE表头(Windows操作系统识别为可执行文件格式,当然不是仅仅一句话这么简单)。
2、CLR表头(专门用于那些需要CLR才能运行的托管模块,包含元数据的主版本和次版本号,如果是CUI或GUI可执行文件还有一个标示入口点方法的MethodDef标记,以及一个可选的强命名数字签名,还有一些元数据表的大小和偏移量,察看SDK/include中的CorHdr.h的IMAGE_COR20_HEADER结构来得到CLR表头的准确格式)
typedef struct IMAGE_COR20_HEADER


{
// Header versioning
ULONG cb;
USHORT MajorRuntimeVersion;//CLR的主版本号
USHORT MinorRuntimeVersion;//CLR的次版本号
// Symbol table and startup information
IMAGE_DATA_DIRECTORY MetaData; //标识元数据在这个PE文件里面的起始位置
ULONG Flags; //标识这个runtime的flags
ULONG EntryPointToken;//MethodDef的入口点,而不是整个托管PE的入口点
// Binding information
IMAGE_DATA_DIRECTORY Resources;
IMAGE_DATA_DIRECTORY StrongNameSignature;

// Regular fixup and binding information
IMAGE_DATA_DIRECTORY CodeManagerTable;
IMAGE_DATA_DIRECTORY VTableFixups;
IMAGE_DATA_DIRECTORY ExportAddressTableJumps;

// Precompiled image info (internal use only - set to zero)
IMAGE_DATA_DIRECTORY ManagedNativeHeader;
} IMAGE_COR20_HEADER;


3、元数据
元数据是一些表的集合,分为3类:定义表,引用表,清单。
定义表:例如ModuleDef,TypeDef,MethodDef,FiledDef,ParamDef,PropertyDef,EventDef......
引用表:例如AssemblyRef,MouuleRef,TypeRef,MemberRef......
清单表:例如AssemblyDef,AssemblyRef,FileDef,ManifestResoureeDef,ExportedTypesDef......
(程序集中存在此表,.netmodule中没有)
清单为程序集的使用者和其各个部分之间提供了一层间接关联,也使得程序集得以实现自描述,清单记录了程序集中所有的其他文件,但是其他文件并不知道他们是某个程序集的一部分。
4.IL代码
程序源代码中中括号{}中的部分被编译成为IL语言。
(附)具体看看hello.cs生成的MetaInfo(通过ILDasm的ctrl+M快捷键来查看)

public class Hello


{
static void Main()

{
System.Console.WriteLine("Hello c# world !");
}
}
ScopeName : Hello.exe

MVID :
{BC0BB08E-E11D-4DAC-866B-A584C5C8CD1A}
===========================================================
Global functions
-------------------------------------------------------

Global fields
-------------------------------------------------------

Global MemberRefs
-------------------------------------------------------

TypeDef #1
-------------------------------------------------------
TypDefName: Hello (02000002)
Flags : [Public] [AutoLayout] [Class] [AnsiClass] (00100001)
Extends : 01000001 [TypeRef] System.Object
Method #1 [ENTRYPOINT]
-------------------------------------------------------
MethodName: Main (06000001)
Flags : [Private] [Static] [HideBySig] [ReuseSlot] (00000091)
RVA : 0x00002050
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: Void
No arguments.

Method #2
-------------------------------------------------------
MethodName: .ctor (06000002)
Flags : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor] (00001886)
RVA : 0x00002068
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.


TypeRef #1 (01000001)
-------------------------------------------------------
Token: 0x01000001
ResolutionScope: 0x23000001
TypeRefName: System.Object
MemberRef #1
-------------------------------------------------------
Member: (0a000003) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.

TypeRef #2 (01000002)
-------------------------------------------------------
Token: 0x01000002
ResolutionScope: 0x23000001
TypeRefName: System.Diagnostics.DebuggableAttribute
MemberRef #1
-------------------------------------------------------
Member: (0a000001) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
2 Arguments
Argument #1: Boolean
Argument #2: Boolean

TypeRef #3 (01000003)
-------------------------------------------------------
Token: 0x01000003
ResolutionScope: 0x23000001
TypeRefName: System.Console
MemberRef #1
-------------------------------------------------------
Member: (0a000002) WriteLine:
CallCnvntn: [DEFAULT]
ReturnType: Void
1 Arguments
Argument #1: String

Assembly
-------------------------------------------------------
Token: 0x20000001
Name : Hello
Public Key :
Hash Algorithm : 0x00008004
Major Version: 0x00000000
Minor Version: 0x00000000
Build Number: 0x00000000
Revision Number: 0x00000000
Locale: <null>
Flags : [SideBySideCompatible] (00000000)
CustomAttribute #1 (0c000001)
-------------------------------------------------------
CustomAttribute Type: 0a000001
CustomAttributeName: System.Diagnostics.DebuggableAttribute :: instance void .ctor(bool,bool)
Length: 6
Value : 01 00 00 01 00 00 > <
ctor args: ( <can not decode> )


AssemblyRef #1
-------------------------------------------------------
Token: 0x23000001
Public Key or Token: b7 7a 5c 56 19 34 e0 89
Name: mscorlib
Major Version: 0x00000001
Minor Version: 0x00000000
Build Number: 0x00001388
Revision Number: 0x00000000
Locale: <null>
HashValue Blob:
Flags: [none] (00000000)


User Strings
-------------------------------------------------------
70000001 : (16) L"Hello c# world !"


posted on 2008-04-18 13:03
谢良威 阅读(39)
评论(0) 编辑 收藏 所属分类:
C# Programing