博客园  :: 首页  :: 联系 :: 管理

C# 2008 学习笔记 - 扩展函数

Posted on 2008-02-19 16:21  sunrack  阅读(2634)  评论(3编辑  收藏  举报
一、介绍
使用扩展函数,可以为无法修改源代码的对象添加新的方法,或者强制让对象支持某些方法,这些方法看起来就是对象本来就有的功能。

二、限制条件
1、必须在static class 中定义,所以必须使用static修饰
2、需要在(只需要在)第一个参数前面使用this修饰
3、扩展函数可以通过对象实例调用,也可以使用定义该函数的静态类直接调用

三、举例:

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,
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);
}
}

四、使用对象实例调用:

static void Main(string[] args)
{
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!");
bool b2 = true;
// Error! Booleans don't have the Foo() method!
// b2.Foo();
Console.ReadLine();
}

五、使用静态类调用:

private static void Main(string[] args)
{
Console.WriteLine(
"***** Fun with Extension Methods *****\n");
int myInt = 12345678;
MyExtensions.DisplayDefiningAssembly(myInt);
DataSet d 
= new DataSet();
MyExtensions.DisplayDefiningAssembly(d);
SoundPlayer sp 
= new SoundPlayer();
MyExtensions.DisplayDefiningAssembly(sp);
Console.WriteLine(
"Value of myInt: {0}", myInt);
Console.WriteLine(
"Reversed digits of myInt: {0}",
MyExtensions.ReverseDigits(myInt));
TesterUtilClass.Foo(myInt);
TesterUtilClass.Foo(myInt, 
"Ints that Foo? Who would have thought it!");
Console.ReadLine();
}



static class TesterUtilClass
{
// Every Int32 now has a Foo() method
public static void Foo(this int i)
{ Console.WriteLine(
"{0} called the Foo() method.", i); }
// which has been overloaded to take a string!
public static void Foo(this int i, string msg)
{ Console.WriteLine(
"{0} called Foo() and told me: {1}", i, msg); }
}

六、扩展函数的使用范围
扩展函数本质上是在被扩展的对象实例上可以调用的静态函数,不是继承,所以不同于普通的成员函数,扩展函数不能直接访问被扩展对象的成员。只能通过该对象的实例来访问。

public class Car
{
public int Speed;
public int SpeedUp()
{
return ++Speed;
}
}



public static class CarExtensions
{
public static int SlowDown(this Car c)
{
// Error! This method is not deriving from Car!
return --Speed;
}
}




public static class CarExtensions
{
public static int SlowDown(this Car c)
{
// OK!
return --c.Speed;
}
}




static void UseCar()
{
Car c 
= new Car();
Console.WriteLine(
"Speed: {0}", c.SpeedUp());
Console.WriteLine(
"Speed: {0}", c.SlowDown());
}


七、引用扩展函数

必须引用定义扩展函数的命名空间,否则扩展函数不可用

八、智能提示

Visual studio 的智能提示将扩展函数标记为向下的蓝色箭头

九、创建扩展函数库
比较好的做法是,定一个类库,在其中实现扩展函数

十、扩展接口类型
// Define a normal CLR interface in C#.
interface IBasicMath
{
int Add(int x, int y);
}
// Implementation of IBasicMath.
class MyCalc : IBasicMath
{
public int Add(int x, int y)
{
return x + y;
}
}

但是,如果只是如下扩展,将会发生错误

static class MathExtensions
{
// Extend IBasicMath with subtraction method?
public static int Subtract(this IBasicMath itf,
int x, int y);
}

必须给出函数的实现

static class MathExtensions
{
// Extend IBasicMath this method and this
// implementation.
public static int Subtract(this IBasicMath itf,
int x, int y)
{
return x - y;
}
}

调用
static void Main(string[] args)
{
Console.WriteLine(
"***** Extending an interface *****\n");
// Call IBasicMath members from MyCalc object.
MyCalc c = new MyCalc();
Console.WriteLine(
"1 + 2 = {0}", c.Add(12));
Console.WriteLine(
"1 - 2 = {0}", c.Subtract(12));
// Can also cast into IBasicMath to invoke extension.
Console.WriteLine("30 - 9 = {0}",
((IBasicMath)c).Subtract(
309));
// This would NOT work!
// IBasicMath itfBM = new IBasicMath();
// itfBM.Subtract(10, 10);
Console.ReadLine();
}

扩展接口后,显然不能直接在接口上调用这些扩展函数,只能理解为,所有继承该接口的对象新增加了这些扩展函数功能,