C# Extension Method
来源:互联网 发布:女友被很多人睡过知乎 编辑:程序博客网 时间:2024/04/28 00:38
C#可以为类创建扩展方法,不过必须是Static类和Static方法,用this关键字加上Class来确定为哪个Class扩展方法。
// Define an interface named IMyInterface. namespace DefineIMyInterface{ using System; public interface IMyInterface { // Any class that implements IMyInterface must define a method // that matches the following signature. void MethodB(); }}// Define extension methods for IMyInterface. namespace Extensions{ using System; using DefineIMyInterface; // The following extension methods can be accessed by instances of any // class that implements IMyInterface. public static class Extension { public static void MethodA(this IMyInterface myInterface, int i) { Console.WriteLine ("Extension.MethodA(this IMyInterface myInterface, int i)"); } public static void MethodA(this IMyInterface myInterface, string s) { Console.WriteLine ("Extension.MethodA(this IMyInterface myInterface, string s)"); } // This method is never called in ExtensionMethodsDemo1, because each // of the three classes A, B, and C implements a method named MethodB // that has a matching signature. public static void MethodB(this IMyInterface myInterface) { Console.WriteLine ("Extension.MethodB(this IMyInterface myInterface)"); } }}// Define three classes that implement IMyInterface, and then use them to test // the extension methods. namespace ExtensionMethodsDemo1{ using System; using Extensions; using DefineIMyInterface; class A : IMyInterface { public void MethodB() { Console.WriteLine("A.MethodB()"); } } class B : IMyInterface { public void MethodB() { Console.WriteLine("B.MethodB()"); } public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); } } class C : IMyInterface { public void MethodB() { Console.WriteLine("C.MethodB()"); } public void MethodA(object obj) { Console.WriteLine("C.MethodA(object obj)"); } } class ExtMethodDemo { static void Main(string[] args) { // Declare an instance of class A, class B, and class C. A a = new A(); B b = new B(); C c = new C(); // For a, b, and c, call the following methods: // -- MethodA with an int argument // -- MethodA with a string argument // -- MethodB with no argument. // A contains no MethodA, so each call to MethodA resolves to // the extension method that has a matching signature. a.MethodA(1); // Extension.MethodA(object, int) a.MethodA("hello"); // Extension.MethodA(object, string) // A has a method that matches the signature of the following call // to MethodB. a.MethodB(); // A.MethodB() // B has methods that match the signatures of the following // method calls. b.MethodA(1); // B.MethodA(int) b.MethodB(); // B.MethodB() // B has no matching method for the following call, but // class Extension does. b.MethodA("hello"); // Extension.MethodA(object, string) // C contains an instance method that matches each of the following // method calls. c.MethodA(1); // C.MethodA(object) c.MethodA("hello"); // C.MethodA(object) c.MethodB(); // C.MethodB() } }}/* Output: Extension.MethodA(this IMyInterface myInterface, int i) Extension.MethodA(this IMyInterface myInterface, string s) A.MethodB() B.MethodA(int i) B.MethodB() Extension.MethodA(this IMyInterface myInterface, string s) C.MethodA(object obj) C.MethodA(object obj) C.MethodB() */
参考Link
http://msdn.microsoft.com/library/bb383977.aspx
0 0
- C#之Extension Method
- C# Extension Method
- C# 之 Extension Method
- c# extension method
- C# 扩展方法Extension Method
- C# constructor chain and extension method
- Extension Method
- Extension Method 和lambda
- Extension Method In .Net
- CSharp - Extension method usage
- C#2008新特性--(扩展方法)Extension Method
- [原创]深入理解C# 3.x的新特性(2):Extension Method - Part II
- [原创]深入理解C# 3.x的新特性(2):Extension Method - Part I
- 扩展Unity3d 组件方法,简化API使用 - C#特性之 Extension Method
- Extension Methods - C#
- C#基础 -- Extension 扩展方法
- Extension Methods (C# 3.0 concept)
- X-HTTP-Method-Override和Http Extension框架
- bloom泛光特效
- cocos2d-x中制作动画
- js 页面关闭前的出现提示的实现代码
- 1014 Specialized Four-Dig
- 微软CRM交流年会【CRM中国论坛 http://www.crmug.cn/】
- C# Extension Method
- 【百度地图API】百度API卫星图使用方法和卫星图对比工具
- 基于haar特征的Adaboost人脸检测技术
- shell 函数
- Filters and Observers in rails
- webkit概况
- cocos2d-x中制作动画
- CMake入门指南
- Blur模糊特效