您的位置:首页 > 编程语言 > C#

C#.Net 扩展方法

2013-10-30 17:58 344 查看
坛子里已经有好多牛人的详细介绍了,下面标记几篇:

鹤冲天的c# 扩展方法奇思妙用

/article/4778931.html

浅析C#扩展方法

http://developer.51cto.com/art/200908/143036.htm

.NET3.0之扩展方法

http://www.cnblogs.com/aspnethot/articles/1385144.html

下面援引".NET3.0之扩展方法"一文, 我就拿来主义懒得截图了:

MSDN对扩展方法的定义: 扩展方法使你能够向现有类型“添加”方法(包括你自定义的类),而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但是可以像扩展类型上的实例方法一样进行调用。对于用C#编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。

LINQ标准查询运算符就是采用的扩展方法方式,见下图



(当我们看到下“小标箭头”的方法就是扩展方法了,这里我们看到OrderBy亦是扩展方法)

看到这里也就是说,扩展方法是为Linq铺路的,是因为要推出Linq才得以享受到扩展方法。

好归好,一定要慎用,不然,一不小心就会污染大面积类的。

别光说不练,自己也写一段,看看里面是啥玩艺:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication6

{

class Program

{

static void Main(string[] args)

{

string abc = "lzd";

abc.ShowLzdInfo();

Console.ReadKey();

}

}

public static class LzdTools

{

public static void ShowLzdInfo(this object s)

{

Console.WriteLine(s);

}

}

}

然后反编译,查查老底:

.method public hidebysig static void ShowLzdInfo(string s) cil managed

{

.custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )

// Code size 9 (0x9)

.maxstack 8

IL_0000: nop

IL_0001: ldarg.0

IL_0002: call void [mscorlib]System.Console::WriteLine(string)

IL_0007: nop

IL_0008: ret

} // end of method LzdTools::ShowLzdInfo

.method private hidebysig static void Main(string[] args) cil managed

{

.entrypoint

// Code size 21 (0x15)

.maxstack 1

.locals init ([0] string abc)

IL_0000: nop

IL_0001: ldstr "lzd"

IL_0006: stloc.0

IL_0007: ldloc.0

IL_0008: call void ConsoleApplication6.LzdTools::ShowLzdInfo(string)

IL_000d: nop

IL_000e: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()

IL_0013: pop

IL_0014: ret

} // end of method Program::Main

原来是给这个方法加了ExtensionAttribute的属性,编译时会根据这个属性来查找扩展方法,然后对号入座。

等到执行时,再替换回原方法ConsoleApplication6.LzdTools::ShowLzdInfo(string)。

干得真是漂亮,给程序员尊严,还不破坏语言的原则。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐