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!");
}
}
}
{
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!");
}
}
}