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)。
干得真是漂亮,给程序员尊严,还不破坏语言的原则。
鹤冲天的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)。
干得真是漂亮,给程序员尊严,还不破坏语言的原则。
相关文章推荐
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法
- 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法
- C#.Net 扩展方法
- [ASP.NET教程] c#写扩展方法
- C#.NET为List加入扩展方法:获取唯一值
- C#.NET为List添加扩展方法:获取唯一值
- c#扩展方法奇思妙用 ASP.NET MVC 篇:巧用扩展方法优先级,美化所有页面 TextBoxFor 文本框
- c# 扩展方法奇思妙用 ASP.NET MVC 篇:巧用扩展方法优先级,美化所有页面 TextBoxFor 文本框
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- C#.NET扩展方法
- C#扩展方法本质
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- C#.Net中获取程序运行时间的两种方法 (转载:陈进学博客)
- Asp.Net中前台Javascript与C#函数相互调方法
- .net c# 中获得GridView的EmptyDataTemplate中的控件的方法(转)
- c# 扩展方法
- C#小技巧(2)之方法扩展
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法
- [转帖]c#.net常用的小函数和方法集