• c#中的扩展方法


    扩展声明

    扩展方法它可以让我们在不修改原始类的情况下,为其添加新的方法。

    并不是任何方法都能作为扩展方法使用 , 必须具有以下特征:
    它必须在一个非嵌套的、非泛型的静态类中(所以必须是一个静态方法)

    至少要有一个参数;

    • 第一个参数必须是要扩展的类型,使用this关键字进行标记
    • 第一个参数不能有其他任何修饰符(比如out或ref);
    • 第一个参数的类型不能是指针类型。

    注意事项

    注意的地方:

    1. 扩展方法不能和调用的方法放到同一个类中
    2. 第一个参数必须要,并且必须是this,这是扩展方法的标识,可以在后面追加参数。
    3. 最好保证扩展方法和调用方法在同一个命名空间下

    实例1

    给string 类型增加一个Add方法,该方法的作用是给字符串增加一个字母a

           //必须是静态类才可以添加扩展方法
           Static class Program
           {
            static void Main(string[] args)
            {
                string str = "quzijing";
                //注意调用扩展方法,必须用对象来调用 
                string Newstr = str.Add();
                Console.WriteLine(Newstr);
                Console.ReadKey();
            }
            //声明扩展方法
            //扩展方法必须是静态的,Add有三个参数
            //this 必须有,string表示我要扩展的类型,stringName表示对象名
            //三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,//再增加一个变量即可
    
            public static  string  Add(this string stringName)
            {
                return stringName+"a";
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    实例2

    使用对象的扩展方法

     public class Person
     {
       public string Name
       {
         set;
         get;
       }
       public int Age
       {
         set;
         get;
       }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    //必须是静态类才可以添加扩展方法
    //Person的扩展方法,根据年龄判断是否是成年人
    public static bool GetBIsChild(this Person oPerson)
    {
      if(oPerson.Age >= 18) return false;
      else return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Main方法里面调用:

    var oPerson1 = new Person();
    oPerson1.Age = 20;
    var bIsChild = oPerson1.GetBIsChild();        
    
    • 1
    • 2
    • 3

    实例3

    泛型对象的扩展

    public static class DataContractExtensions
    {
      //必须是静态类才可以添加扩展方法
      //测试方法
      public static T Test < T > (this T instance) where T: Test2
      {
        T Res =
          default(T);
        try
        {
          Res.AttrTest = instance.AttrTest.Substring(0, 2);
          //其他复杂逻辑...
        }
        catch
        {}
        return Res;
      }
    }
    public class Test2
    {  
      public string AttrTest
      {
        set;
        get;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    实例4

    集合扩展方法

     public static void ShowItems < T > (this IEnumerable < T > _al)
     {
       foreach(var item in _al)
       {
         Console.WriteLine(item);
       }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
     static void Main(string[] args)
     {
       //调用集合的扩展方法
       str.ShowItems < char > ();
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    vue+electron 修改默认安装目录
    【Adobe Illustrator 教程】4. 认识渐变工具
    supervisord 进程管理器 Laravel执行队列
    Java编程语言是什么传递,即值传递和引用传递的区别
    移动测试自动化的客户端与云端执行
    Centos7安装Jenkins
    21-CSS中的3D属性
    【JavaEE进阶】Spring统一功能处理:拦截器的使用
    Java -- 每日一问:后台服务出现明显“变慢”,谈谈你的诊断思路?
    vue app开发调用原生方法实现权限访问授权处理(一)
  • 原文地址:https://blog.csdn.net/qq_35624605/article/details/130898298