扩展方法(C#)

来源:互联网 发布:编程的基本逻辑 编辑:程序博客网 时间:2024/06/05 20:31

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

最常见的扩展方法是 LINQ 标准查询运算符,这些运算符在现有 System.Collections..::.IEnumerable 和 System.Collections.Generic..::.IEnumerable<(Of <(T>)>) 类型中添加了查询功能。若要使用这些标准查询运算符,请先使用 using System.Linq 指令将它们纳入范围中。然后,任何实现了 IEnumerable<(Of <(T>)>) 的类型看起来都具有 GroupBy、OrderBy、Average 等实例方法。在 IEnumerable<(Of <(T>)>) 类型的实例(如 List<(Of <(T>)>) 或 Array)后键入“点”时,可以在 IntelliSense 语句结束中看到这些附加方法。

下面的示例演示如何对一个整数数组调用标准查询运算符 OrderBy 方法。括号里面的表达式是一个 lambda 表达式。很多标准查询运算符采用 lambda 表达式作为参数,但这不是扩展方法的必要条件。有关更多信息,请参见 Lambda 表达式(C# 编程指南)。

  1. class ExtensionMethods2     
  2. {  
  3.   
  4.     static void Main()  
  5.     {             
  6.         int[] ints = { 10, 45, 15, 39, 21, 26 };  
  7.         var result = ints.OrderBy(g => g);  
  8.         foreach (var i in result)  
  9.         {  
  10.             System.Console.Write(i + " ");  
  11.         }            
  12.     }         
  13. }  
  14. //Output: 10 15 21 26 39 45  

扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。仅当您使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

下面的示例演示为 System..::.String 类定义的一个扩展方法。请注意,它是在非嵌套、非泛型静态类内部定义的:

  1. namespace ExtensionMethods  
  2. {  
  3.     public static class MyExtensions  
  4.     {  
  5.         public static int WordCount(this String str)  
  6.         {  
  7.             return str.Split(new char[] { ' ''.''?' }, StringSplitOptions.RemoveEmptyEntries).Length;  
  8.         }  
  9.     }    
  10. }  

可使用以下 using 指令将 WordCount 扩展方法放入范围中:

  1. using ExtensionMethods;  

而且,可以在应用程序中使用以下语法对该扩展方法进行调用:

  1. string s = "Hello Extension Methods";  
  2. int i = s.WordCount();  

在代码中,可以使用实例方法语法调用该扩展方法。但是,编译器生成的中间语言 (IL) 会将代码转换为对静态方法的调用。因此,并未真正违反封装原则。实际上,扩展方法无法访问它们所扩展的类型中的私有变量。

有关更多信息,请参见如何:实现和调用自定义扩展方法(C# 编程指南)。

通常,您更多时候是调用扩展方法而不是实现您自己的扩展方法。由于扩展方法是使用实例方法语法调用的,因此不需要任何特殊知识即可从客户端代码中使用它们。若要为特定类型启用扩展方法,只需为在其中定义这些方法的命名空间添加 using 指令。例如,若要使用标准查询运算符,请将以下 using 指令添加到代码中:

  1. using System.Linq;  

(您可能还必须添加对 System.Core.dll 的引用。)您将注意到,标准查询运算符现在作为可供大多数 IEnumerable<(Of <(T>)>) 类型使用的附加方法显示在 IntelliSense 中。
[说明] 说明:

尽管标准查询运算符没有显示在 String 的 IntelliSense 中,但它们仍然可用。
 在编译时绑定扩展方法

可以使用扩展方法来扩展类或接口,但不能重写扩展方法。与接口或类方法具有相同名称和签名的扩展方法永远不会被调用。编译时,扩展方法的优先级总是比类型本身中定义的实例方法低。换句话说,如果某个类型具有一个名为 Process(int i) 的方法,而您有一个具有相同签名的扩展方法,则编译器总是绑定到该实例方法。当编译器遇到方法调用时,它首先在该类型的实例方法中寻找匹配的方法。如果未找到任何匹配方法,编译器将搜索为该类型定义的任何扩展方法,并且绑定到它找到的第一个扩展方法。下面的示例演示编译器如何确定要绑定到哪个扩展方法或实例方法。
 示例

下面的示例演示 C# 编译器在确定是将方法调用绑定到类型上的实例方法还是绑定到扩展方法时所遵循的规则。静态类 Extensions 包含为任何实现了 IMyInterface 的类型定义的扩展方法。类 A、B 和 C 都实现了该接口。

MethodB 方法永远不会被调用,因为它的名称和签名与这些类已经实现的方法完全匹配。

如果编译器找不到具有匹配签名的实例方法,它会绑定到匹配的扩展方法(如果存在这样的方法)。

  1. namespace Extensions  
  2. {  
  3.   using System;  
  4.   using ExtensionMethodsDemo1;  
  5.   
  6.      // Define extension methods for any type that implements IMyInterface.  
  7.      public static class Extension  
  8.      {  
  9.         public static void MethodA(this IMyInterface myInterface, int i)  
  10.         {  
  11.             Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");  
  12.         }  
  13.   
  14.         public static void MethodA(this IMyInterface myInterface, string s)  
  15.         {  
  16.             Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");  
  17.         }  
  18.   
  19.         // This method is never called, because the three classes implement MethodB.  
  20.         public static void MethodB(this IMyInterface myInterface)  
  21.         {  
  22.             Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");  
  23.         }  
  24.     }  
  25. }  
  26. namespace ExtensionMethodsDemo1  
  27. {  
  28.     using System;  
  29.     using Extensions;  
  30.   
  31.     public interface IMyInterface  
  32.     {  
  33.         void MethodB();  
  34.     }  
  35.   
  36.     class A : IMyInterface  
  37.     {  
  38.         public void MethodB(){Console.WriteLine("A.MethodB()");}  
  39.     }  
  40.   
  41.     class B : IMyInterface  
  42.     {  
  43.         public void MethodB() { Console.WriteLine("B.MethodB()"); }  
  44.         public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }  
  45.     }  
  46.   
  47.     class C : IMyInterface  
  48.     {  
  49.         public void MethodB() { Console.WriteLine("C.MethodB()"); }  
  50.         public void MethodA(object obj) { Console.WriteLine("C.MethodA(object obj)"); }  
  51.     }  
  52.   
  53.     class ExtMethodDemo  
  54.     {  
  55.         static void Main(string[] args)  
  56.         {  
  57.             A a = new A();  
  58.             B b = new B();  
  59.             C c = new C();  
  60.             TestMethodBinding(a,b,c);  
  61.         }  
  62.   
  63.         static void TestMethodBinding(A a, B b, C c)  
  64.         {  
  65.             // A has no methods, so each call resolves to  
  66.             // the extension methods whose signatures match.  
  67.             a.MethodA(1);           // Extension.MethodA(object, int)  
  68.             a.MethodA("hello");     // Extension.MethodA(object, string)  
  69.             a.MethodB();            // A.MethodB()  
  70.   
  71.             // B itself has a method with this signature.  
  72.             b.MethodA(1);           // B.MethodA(int)  
  73.             b.MethodB();            // B.MethodB()  
  74.   
  75.             // B has no matching method, but Extension does.  
  76.             b.MethodA("hello");     // Extension.MethodA(object, string)  
  77.   
  78.             // In each case C has a matching instance method.  
  79.             c.MethodA(1);           // C.MethodA(object)  
  80.             c.MethodA("hello");     // C.MethodA(object)  
  81.             c.MethodB();            // C.MethodB()  
  82.         }  
  83.     }  
  84. }  
  85. /* Output: 
  86.     Extension.MethodA(this IMyInterface myInterface, int i) 
  87.     Extension.MethodA(this IMyInterface myInterface, string s) 
  88.     A.MethodB() 
  89.     B.MethodA(int i) 
  90.     B.MethodB() 
  91.     Extension.MethodA(this IMyInterface myInterface, string s) 
  92.     C.MethodA(object obj) 
  93.     C.MethodA(object obj) 
  94.     C.MethodB() 
  95.  */  

 通用准则

通常,建议您只在不得已的情况下才实现扩展方法,并谨慎地实现。只要有可能,必须扩展现有类型的客户端代码都应该通过创建从现有类型派生的新类型来达到这一目的。有关更多信息,请参见继承(C# 编程指南)。

在使用扩展方法来扩展您无法更改其源代码的类型时,您需要承受该类型实现中的更改会导致扩展方法失效的风险。

如果您确实为给定类型实现了扩展方法,请记住以下两点:

    *      如果扩展方法与该类型中定义的方法具有相同的签名,则扩展方法永远不会被调用。
    *      扩展方法被在命名空间级别放入范围中。例如,如果您在同一个名为 Extensions 的命名空间中具有多个包含扩展方法的静态类,则这些扩展方法将全部由 using Extensions; 指令放入范围中。

类库的实施者不应使用扩展方法来避免创建程序集的新版本。如果您要向库中添加重要的新功能,并且您拥有源代码,则应该遵循标准 .NET Framework 程序集版本控制准则。有关更多信息,请参见程序集版本控制。

如何:实现和调用自定义扩展方法(C# 编程指南)

客户端代码可通过以下方式使用您的扩展方法:添加对包含这些扩展方法的 DLL 的引用,并且添加一条 using 指令以指定在其中定义这些扩展方法的命名空间。
定义和调用扩展方法

1.      定义一个静态类以包含扩展方法。

      该类必须对客户端代码可见。有关可访问性规则的更多信息,请参见访问修饰符(C# 编程指南)。

2.      将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。

3.      该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。

4.      在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。

5.      按照与调用类型上的实例方法一样的方式调用扩展方法。

      请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。您只需通过 n 为这两个形参提供实参。

下面的示例在 MyExtensions.StringExtension 类中实现了一个名为 WordCount 的扩展方法。该方法对 String 类进行操作,而该类被指定为第一个方法参数。MyExtensions 命名空间被导入到应用程序命名空间中,并且该方法是在 Main 方法内调用的。

  1. using System.Linq;  
  2. using System.Text;  
  3. using System;  
  4.   
  5. namespace CustomExtensions  
  6. {  
  7.     //Extension methods must be defined in a static class  
  8.     public static class StringExtension  
  9.     {  
  10.         // This is the extension method.  
  11.         // The first parameter takes the "this" modifier  
  12.         // and specifies the type for which the method is defined.  
  13.         public static int WordCount(this String str)  
  14.         {  
  15.             return str.Split(new char[] {' ''.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;  
  16.         }  
  17.     }  
  18. }  
  19. namespace Extension_Methods_Simple  
  20. {  
  21.     //Import the extension method namespace.  
  22.     using CustomExtensions;  
  23.     class Program  
  24.     {  
  25.         static void Main(string[] args)  
  26.         {  
  27.             string s = "The quick brown fox jumped over the lazy dog.";  
  28.             //  Call the method as if it were an  
  29.             //  instance method on the type. Note that the first  
  30.             //  parameter is not specified by the calling code.  
  31.             int i = s.WordCount();  
  32.             System.Console.WriteLine("Word count of s is {0}", i);  
  33.         }  
  34.     }  

原创粉丝点击