下表显示了在编译时进行的方法转换
| 方法 | 代码编译为 | |
| 1 | expr . identifier ( ) | identifier (expr) |
| 2 | expr . identifier ( args ) | identifier (expr, args) |
| 3 | expr . identifier <typeargs> ( ) | identifier <typeargs> (expr) |
| 4 | expr . identifier <typeargs> ( args ) | identifier <typeargs> (expr, args) |
如果你在ILDASM中检查main方法的代码,它将会如下显示:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 42 (0x2a)
.maxstack 1
.locals init ([0] string s,
[1] int32 i,
[2] int32 j)
IL_0000: nop
IL_0001: ldstr "9"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call int32 ExtensionMethods.EMClass::ToInt32Ext(string)
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: call void [mscorlib]System.Console::WriteLine(int32)
IL_0014: nop
IL_0015: ldloc.0
IL_0016: call int32 ExtensionMethods.EMClass::
ToInt32Static(string)
IL_001b: stloc.2
IL_001c: ldloc.2
IL_001d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0022: nop
IL_0023: call string [mscorlib]System.Console::ReadLine()
IL_0028: pop
IL_0029: ret
} // end of method Program::Main
IL_0008: call int32 ExtensionMethods.EMClass::
ToInt32Ext(string)
这里表明方法转换(expr . identifier ( ) <--> identifier (expr) )发生.
所以当你调用 int i = s.ToInt32Ext();, 编译器内部进行操作int i = EMClass.ToInt32Ext(s);那么,重写的新生会当作一个静态方法调用来处理
标识符按照如下的顺序解析:
1. 最近的包含的命名空间声明
2. 每个后继包含的命名空间声明
3. 包含的编译单元
下面是方法的从高到低的优先级:
1. 实例方法
2. 在同一个命名空间里的扩展方法
3. 在当前命名空间之外的扩展方法
为什么使用扩展方法?
你也许会问:"为什么有了普通的静态和实例类还需要使用扩展方法呢?"其实,简单来说就是为了方便。我来举个例子吧。如果你在过去的一段时间内开发了很多函数形成了一个库。那么当某人要用这个函数库的时候,他必须要知道定义了所需的静态方法的类名。就像下面这个一样:
a = MyLibraryClass.
这里,IntelliSense将会弹出并且告诉你可用函数的名字,你只需要挑选你所需要的。然后键入你需要的方法名和相关参数。
a = MyLibraryClass.DesiredFunction(strName)
使用这种方法,你必须事先知道哪个库包含了你所要的哪个函数和它的名称,使用扩展方法就不一样了:
a = strName.
这里,IntelliSense将会弹出,并且显示可以使用哪些扩展方法。你只需要键入你需要的扩展方法:
a = strName.DesiredFunction()
这里无须给出所需的参数名来指定数据类型。
在对象实例中调用静态方法
扩展方法提供了一个新的机制用来在对象实例上调用静态方法。但和实例方法比较起来,它还是在功能上有诸多限制,因此你应该保守的使用它,主要将它哦能够在实例方法能力所不及的地方。
C# 3.0并不是一个正式的版本,所以它的标准也没有最后定稿。因此,这样的格式也很有可能变化。
