【Emit基础】IL定义方法的语法详解

     在IL中,方法的定义语法:
.method <flags> <call_conv> <ret_type> <name>(<arg_list><impl>
{
  
<method_body>
}


一.Flags部分可设置
1.可访问性

  • privatescope: 该方法不能被引用。
  • private: 该方法只能被自身及其嵌套类型调用。
  • famandassem: 该方法只能被当前程序集内部的继承类型调用。(internal and protected)
  • assembly: 该方法只能被程序集内部成员调用。
  • family: 该方法只能被继承类型调用。
  • famorassem: 该方法可以被程序集内部成员或外部继承类型调用。(internal or protected)
  • public: 公共方法。
2. Contract
  • static: 静态方法。
  • final: 不能被 override 的方法。
  • virtual: 虚方法,不能和 static 同时使用。
  • hidebysig: 该方法对继承类型隐藏。该标记仅提供给编译器使用,但会被 CLR 忽略。
3. Vitual method table (v-table) control flags
  • newslot: 必须和 virtual 一起使用,添加该关键字的虚方法不会 override 基类中同名(含签名)虚方法,而是在 v-table 中创建一个新槽(new slot)。
4. Implementation
  • abstract: 抽象方法,不提供实现代码。其 RVA = 0,任何拥有抽象方法的类型必须添加 abstract 成为抽象类型。
  • specialname: 该方法名有特殊含义,如 .cctor、.ctor 等。
  • pinvokeimpl: 该方法是非托管方法,通过 P/Invoke 封装调用。
5. Reserved
  • rtspecialname: 该方法拥有 CLR 内部使用的特殊名称,必须和 specialname 一起使用。
二. Implementation Flags

1. Code type
  • cil: 该方法由 CIL (common intermediate language) 实现。(默认)
  • native: 该方法由 native code 实现。
  • runtime: 该方法由 CLR 自动生成。只有 mscorlib.dll 中的某些方法带有此标记。
2. Code management
  • managed: 托管代码。(默认)
  • unmanaged: 非托管代码,必须和 native 一起使用。
3. Implementation and interoperability
  • internalcall: 内部使用标记。
  • synchronized: 指示 JIT 自动为其插入同步代码,以保证多线程调用安全。在 C# 中我们为方法添加 [MethodImpl(MethodImplOptions.Synchronized)] 特性时,编译器就会为该方法添加该标记。对于实例方法而言,自动插入代码会锁定实例对象引用;而静态方法,会锁定其所在类型的静态类型对象(System.Type)。
  • noinlining: 禁止内联(inline)调用。

 

posted @ 2008-09-23 14:30  zhuweisky  阅读(2578)  评论(0编辑  收藏  举报