OMG, I was limited anything
博客园
::
首页
:: ::
联系
:: ::
管理
::
[笔记] C# 3.0 新特性[2]-Understanding Extension Methods
namespace
net30netfeature.extendMethod
{
using
System;
static
class
MyExtensions
{
//
This method allows any object to display the assembly
//
it is defined in.
public
static
void
DisplayDefiningAssembly(
this
object
obj)
{
Console.WriteLine(
"
{0} lives here:\n\t->{1}\n
"
, obj.GetType().Name,
System.Reflection.Assembly.GetAssembly(obj.GetType()));
}
//
This method allows any integer to reverse its digits.
//
For example, 56 would return 65.
public
static
int
ReverseDigits(
this
int
i)
{
//
Translate int into a string, and then
//
get all the characters.
char
[] digits
=
i.ToString().ToCharArray();
//
Now reverse items in the array.
Array.Reverse(digits);
//
Put back into string.
string
newDigits
=
new
string
(digits);
//
Finally, return the modified string back as an int.
return
int
.Parse(newDigits);
}
//
Every Int32 now has a Foo() method
//
[System.Runtime.CompilerServices.Extension]
internal
static
void
Foo(
this
int
i)
{ Console.WriteLine(
"
{0} called the Foo() method.
"
, i); }
//
which has been overloaded to take a string!
internal
static
void
Foo(
this
int
i,
string
msg)
{ Console.WriteLine(
"
{0} called Foo() and told me: {1}
"
, i, msg); }
}
}
namespace
net30netfeature
{
using
System;
//
Importing Types That Define Extension Methods
using
net30netfeature.extendMethod;
public
class
Extentionmethod
{
/**/
/*
*就你所知,一个类型一但被定义编译后,就定型了。要想添加新功能方法只能通过继承,修改原代码,或通过System.Reflection.Emit
*进行反射注入进行动态编译。
* C# 3.0的扩展方法特性使这一需求成为了可能,扩展方法可以扩展已经存在的编译类型,使之添加新的成员,
* 而不用更新原有的类型。
* 这是非常有用的,当你需要注入新的功能到已经存在的类型中时,使用扩展方法,你可以添加新的功能到已经编译的类型中,
* 来提供一个幻影,如同已经存在的类型具有这个功能方法。
* 定义扩展方法时有三个限制:
* 1)方法必须定义在静态类中,每个扩展方法也必须是静态方法。
* 2)扩展方法的第一个参数必须用this关键定进行标识。
* 3)扩展方法可以通过相应的实例方式或通过静态类方式进行调用。
*/
}
public
static
class
TesterUtilClass
{
public
static
void
Test()
{
Test2();
}
/**/
///
<summary>
///
Invoking Extension Methods from an Instance Level
///
</summary>
public
static
void
Test1()
{
Console.WriteLine(
"
***** Fun with Extension Methods *****\n
"
);
//
The int has assumed a new identity!
int
myInt
=
12345678
;
myInt.DisplayDefiningAssembly();
//
So has the DataSet!
System.Data.DataSet d
=
new
System.Data.DataSet();
d.DisplayDefiningAssembly();
//
And the SoundPlayer!
System.Media.SoundPlayer sp
=
new
System.Media.SoundPlayer();
sp.DisplayDefiningAssembly();
//
Use new integer functionality.
Console.WriteLine(
"
Value of myInt: {0}
"
, myInt);
Console.WriteLine(
"
Reversed digits of myInt: {0}
"
, myInt.ReverseDigits());
myInt.Foo();
myInt.Foo(
"
Ints that Foo? Who would have thought it!
"
);
//
Error! Booleans don't have the Foo() method!
bool
b2
=
true
;
//
b2.Foo();
}
/**/
///
<summary>
///
Invoking Extension Methods Statically
///
</summary>
public
static
void
Test2()
{
/**/
/*
* 回忆一下扩展方法的第一个参数被标记为this关键字,其后跟随着方法被应用的类型,如果我们看一下
* 这个背后发生了什么,可以用 ildasm.exe or Lutz Roeder’s Reflector来进行查看,我们会发现,编译器
* 把实例的扩展方法的调用转换成静态方法的调用。和如下的方法的调用类似。
*/
Console.WriteLine(
"
***** Fun with Extension Methods *****\n
"
);
int
myInt
=
12345678
;
MyExtensions.DisplayDefiningAssembly(myInt);
System.Data.DataSet d
=
new
System.Data.DataSet();
MyExtensions.DisplayDefiningAssembly(d);
System.Media.SoundPlayer sp
=
new
System.Media.SoundPlayer();
MyExtensions.DisplayDefiningAssembly(sp);
Console.WriteLine(
"
Value of myInt: {0}
"
, myInt);
Console.WriteLine(
"
Reversed digits of myInt: {0}
"
,
MyExtensions.ReverseDigits(myInt));
MyExtensions.Foo(myInt);
MyExtensions.Foo(myInt,
"
Ints that Foo? Who would have thought it!
"
);
}
}
}
posted on 2008-02-20 12:25
共同学习,共同进步
阅读(57)
评论(0)
编辑
收藏
网摘
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-02-20 13:10 编辑过
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索
相关文章:
温故知新ASP.NET 2.0(C#)系列
什么是C#及.NET FrameWork
prototype.js 1.4版开发者手册(强烈推荐)
.NET设计模式系列文章
和 C#4.0 团队面对面
Flash Player 10 的新特性
求 C#3.0 PPT 课件
关于C#3.0中新关键字var
《C# 3.0 Unleashed》征集译者
C#和.NET 2.0实战-平台、语言与框架
相关链接:
最新IT新闻:
OpenOffice.org 3.0.0发布
Linux终于迎来了“同等”的Flash播放器
百度和讯全财经网正式上线
微软将于明天召开Blue Hat安全大会
超强的支付宝Flash招聘广告
与我联系
发短消息
搜索
留言簿
(7)
给我留言
查看留言
我参加的小组
LumaQQ.NET
.NET 3.x
SharePoint
你必须知道的.NET
Web技术联盟
WCF技术研究
Microsoft New Skill
框架设计
易书阁
我参与的团队
SharePoint团队(0/747)
O/R Mapping团队(0/209)
Windows Vista开发(0/240)
随笔分类
(1)
AxAppModule
(rss)
AxTechModule
(rss)
收藏(1)
(rss)
Powered by:
博客园
Copyright © 共同学习,共同进步