日期:2008-05-08  浏览次数:20514 次

好久没在这贴原创的帖子了,尤其是用中文:)这次本来是想和bigeagle探讨一下CLR的,但是,等到动笔时我又改变了主意,主题更为广泛,包括ASP.Net,XML,CLR和IL也许还有Web Service,呵呵,其原因有二:一是CLR这个话题太枯燥,很多朋友没有兴趣,没人理睬的文章我也没有写下去的动力;二是我想我写的越多,暴露的缺点也越多,可以从大家那里获得很多指点,我会获益匪浅(有点自私啊:))。这次计划写很长,我自己也没什么把握,呵呵。

言归正传
第一章Assemblies
sigh,也是很没劲的话题,你可能会想。但也许你咬着牙读完的时候,会发现它其实很有趣。OK,Let’s begin。CLR(Common Language Runtime)要求所有的类型都要归属于一个Assembly。Assemblies在.Net Framework中的角色是组件代码的最小可分发单位。一些关于Assemblies的要点
l         Assemblies是类型定义的集合
l         类型名是由它所处的Assembly检查的
l         只有标记为public的类型和成员才能在Assembly的外部可见
l         标记为internal的成员和类型在Assembly的外部不能访问
l         标记为private的成员在声明它们的类型的外部是不可见的
1.1
Assemblies和modules
一个Assembly包含一个或多个物理文件(modules),module是一个包括代码和metadata的可执行文件,每个module都可能是用不同的语言生成的,在一个Assembly里一个module包含一份Assembly manifest。Csc.exe /t:module和/addmodule开关支持module级的编译和引用。默认的情况下,csc.exe /t:module生成的文件会有一个.netmodule的扩展名。这时,它是不包含manifest的,这种文件是不能被CLR加载的。然而这样的文件却可以通过/addmodule开关整合到assembly manifest中。而al.exe可以连接(link)多个module并创建manifest。
/t:module                                                 

.module test.netmodule

.assembly extern mscorlib
.assembly extern Michael

.class public PP{
.field [michael]Bassist wangpeng
}

/t:library

.module test.dll

.assembly extern mscorlib
.assembly extern Michael

.class public PP{
.field [michael]Bassist wangpeng
}

.assembly test(manifest)


再举一个multi-module assembly的例子

/t:library /addmodule:winform.netmodule, webservice.netmodule

.module forum.dll

.assembly extern mscorlib
.assembly extern Joy
.assembly extern Bigeagle

.class public Master{}

.assembly forum(由此向下是manifest并注意下两个表格与它的关系)

.file winform.netmodule
.file webservice.netmodule

.class extern public WS{
.file webservice.netmodule
.class nnnn
}
.class extern public WF{
.file winform.netmodule
.class nnnn
}

/t:module

.module webservice.netmodule

.assembly extern mscorlib
.assembly extern Bigeagle

.class extern public WS{
.field [Bigeagle] Bassist m
}

/t:module

.module winform.netmodule

.assembly extern mscorlib
.assembly extern Joy

.class extern public WF{
.field [Joy] Singer m
}

Code.cs→csc.exe /t:module→code.netmodule
Component.cs→cscs.exe /t:library /addmodule:code.netmodule→component.dll

Component Assembly

code.netmodule

component.dll


Application.cs→csc /r:component.dll→ap