..net 3.5新特性之用this关键字为类添加扩展方法

 

具体用法如下:

[c-sharp] view plain copy
 
  1. public static class ClassHelper  
  2. {  
  3.     //用this 声明将要吧这个方法附加到Student对象  
  4.     public static bool CheckName(this Student stu)  
  5.     {  
  6.         if (stu.Name == "小明")  
  7.         {  
  8.             return true;  
  9.         }  
  10.         else  
  11.             return false;  
  12.     }  
  13.     //为String对象添加一个ChangeString 方法  
  14.     public static string ChangeString(this String s,string str)  
  15.     {  
  16.         return str + ":" + s;  
  17.     }  
  18. }  
  19.   
  20. public class Student  
  21. {  
  22.     public string Name { get; set; }  
  23.   
  24.     public void Show()  
  25.     {  
  26.         this.CheckName();//Student拥有了CheckName这个方法  
  27.           
  28.     }  
  29.     public void ShowString()  
  30.     {  
  31.         string s = "aaa";  
  32.         s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  33.     }  
  34. }  

这个例子很简单,为自定义的Student类扩展了一个CheckName方法,该方法没有参数,然后又为String系统自带的类 扩展了一个ChangeString 方法,该方法有一个string  类型的参数,注意:扩展方法必须声明为static静态方法,并且放在静态类中。

this不仅可以扩展类方法,还可以扩展接口,使得任何继承自该接口的类都会拥有此扩展方法。这里我们修改上面这个例子:

[c-sharp] view plain copy
 
  1. public static class ClassHelper  
  2.     {  
  3.         //用this 声明将要吧这个方法附加到IStudent对象  
  4.         public static bool CheckName(this IStudent stu)  
  5.         {  
  6.             if (stu.Name == "小明")  
  7.             {  
  8.                 return true;  
  9.             }  
  10.             else  
  11.                 return false;  
  12.         }  
  13.         //为String对象添加一个ChangeString 方法  
  14.         public static string ChangeString(this String s, string str)  
  15.         {  
  16.             return str + ":" + s;  
  17.         }  
  18.   
  19.   
  20.     }  
  21.   
  22.     public interface IStudent  
  23.     {  
  24.         public void Show();  
  25.         public string Name { get; set; }  
  26.     }  
  27.   
  28.     public class StudentA : IStudent  
  29.     {  
  30.           
  31.   
  32.         public void Show()  
  33.         {  
  34.             this.CheckName();//Student拥有了CheckName这个方法  
  35.   
  36.         }  
  37.         public void ShowString()  
  38.         {  
  39.   
  40.             string s = "aaa";  
  41.             s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  42.               
  43.         }  
  44.     }  
  45.   
  46.     public class StudentB : IStudent  
  47.     {  
  48.   
  49.   
  50.         public void Show()  
  51.         {  
  52.             this.CheckName();//Student拥有了CheckName这个方法  
  53.   
  54.         }  
  55.         public void ShowString()  
  56.         {  
  57.             string s = "aaa";  
  58.             s.ChangeString("哈哈");//String拥有了ChangeString这个方法  
  59.   
  60.         }  
  61.     }  
,注意这个例子的CheckName由this Student 改成了 this IStudent,IStudent 是一个接口,StudentA和StudentB都继承了这个接口,他们都会拥有CheckName这个扩展方法。

显然这样做是有好处的,当第三方提供给你一个DLL,其中包括许多类,由于这些类都是封装在DLL中的,当你想扩展其中某个类的时候,你却不能修改类的源代码。这时候你就可以用this来扩展。。。。

原文地址:https://www.cnblogs.com/dxqNet/p/8195019.html