代码改变世界

Apress.Expert.IL 2.0 关键部分零星积累

2011-08-06 21:41  symphony2010  阅读(168)  评论(0编辑  收藏  举报

You have probably noticed that the declaration of TypeDef in the sample contains three
default items: the flags auto and ansi and the extends clause. Yes, in fact, I could have declared
the same TypeDef as .class public Even { ... }, but then I would not be able to discuss the
TypeDef flags and the extends clause.
Finally, I must emphasize one important fact about the class declaration in ILAsm.
(Please pay attention, and don’t say I haven’t told you!) Some languages require that all of a
class’s attributes and members be defined within the lexical scope of the class, defining the
class as a whole in one place. In ILAsm the class needn’t be defined all in one place.
In ILAsm, you can declare a TypeDef with some of its attributes and members, close the
TypeDef’s scope, and then reopen the same TypeDef later in the source code to declare more of
its attributes and members. This technique is referred to as class amendment.

你可能注意到,在该例子的类型定义中包含三个默认的项:auto,ansi,extends. 是的,我可以声明类似的定义如.class public Even,

但是我不能讨论该标志的定义.

最后,我必须强调一件重要的事实,一些语言要求所有类的属性和成员应被定义在类的词汇的范围内,定义一个类在一个地方.在ILAsm中,

该类没必要定义在一个地方.在ILAsm中,你可以生命一个类型定义和他的一些属性和成员在它的范围内,然后重新打开相同的类型定义

在源代码中,来声明更多的属性和成员.这种技术叫做类的修正.

When you amend a TypeDef, the flags, the extends clause, and the implements clause (not
discussed here in the interests of keeping the sample simple) are ignored. You should define
these characteristics of a TypeDef the first time you declare it.
There is no limitation on the number of TypeDef amendments or on how many source
files a TypeDef declaration might span. You are required, however, to completely define a
TypeDef within one module. Thus, it is impossible to amend the TypeDefs defined in other
assemblies or other modules of the same assembly.

当你修改一个类型定义,标志,扩展语句,和执行语句(保证例子的简单性的好处在这里没有被讨论)被忽略.你应该定义TypeDef的特点在你第一次声明时.

没有对TypeDef的修改文件的数量和跨度进行限制因此,无法对别的程序集或者别的模块里面的TypeDef进行修改.

Chapter 7 provides detailed information about ILAsm class declarations..USING PSEUDOFLAGS TO DECLARE A VALUE TYPE AND AN ENUMERATION
You might want to know about a little cheat that will allow you to circumvent the necessity of repeating the
extends clause. ILAsm has two keywords, value and enum, that can be placed among the class flags to
identify, respectively, value types and enumerations if you omit the extends clause.

你可能想知道一些关于如何规避重复的扩展语句.ILAsm有两个关键字,value和enum,能够被放在class标志之间来辨别和区分值类型和枚举类型如果你省略扩展子句.

(If you include the
extends clause, these keywords are ignored.) This is, of course, not a proper way to represent the meta-data, because it can give the incorrect impression that value types and enumerations are identified by certain
TypeDef flags. I am ashamed that ILAsm contains such lowly tricks, but I am too lazy to type extends
[mscorlib]System.ValueType again and again. ILDASM never resorts to these cheats and always
truthfully prints the extends clause, but ILDASM has the advantage of being a software utility.
Field Declaration
This is the field declaration of the OddOrEven application:
.field public static int32 val
.field public static int32 val defines a metadata item named Field Definition
(FieldDef). Because the declaration occurs within the scope of class Odd.or.Even, the declared
field belongs to this class.
The keywords public and static define the flags of the FieldDef. The keyword public
identifies the accessibility of this field and means the field can be accessed by any member for
whom this class is visible. Alternative accessibility flags are as follows:
• The assembly flag specifies that the field can be accessed from anywhere within this
assembly but not from outside.
• The family flag specifies that the field can be accessed from any of the classes descend-ing from Odd.or.Even.
• The famandassem flag specifies that the field can be accessed from any of those descen-dants of Odd.or.Even that are defined in this assembly.
• The famorassem flag specifies that the field can be accessed from anywhere within this
assembly as well as from any descendant of Odd.or.Even, even if the descendant is
declared outside this assembly.
• The private flag specifies that the field can be accessed from Odd.or.Even only.
CHAPTER 1 

■ SIMPLE SAMPLE 11
• The privatescope flag specifies that the field can be accessed from anywher