zoukankan      html  css  js  c++  java
  • ..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来扩展。。。。

  • 相关阅读:
    你好,2021!
    庚子走,辛丑来,愿一切安好!
    花魂鸟魂总难留,鸟自无言花自羞
    熟悉的小胡同
    夜半听鼾声
    写在儿子22岁生日
    vue配置Azure Artifacts npm包源
    RabbitMQ出错:AMQP close-reason, initiated by Peer, code=530的解决办法
    .NET MVC存储图片到数据库的解决方案
    EF Core解决报错’EntityTypeBuilder未包含“ToTable”的定义‘的方法
  • 原文地址:https://www.cnblogs.com/dxqNet/p/8195019.html
Copyright © 2011-2022 走看看