在 OOPL 中,有静态方法、实例方法和虚方法,如下:
 
public sealed class String
{
     public static bool  IsNullOrEmpty(string s)
     {
          // ...
     }
 
     public string Replace(string old, string new)
     {
          // ...
     }
}
public abstract class Stream
{
     public virtual void WriteByte(byte value)
     {
          // ...
     }
}
 
其中,IsNullOrEmpty 是静态方法,Replace 是实例方法,而 WriteByte 就是虚方法。其用法如下:
 
String name = null;
Console.WriteLine(String.IsNullOrEmpty(name));      // true
 
String s = "Hello, world!";
Console.WriteLine(s.Replace("world", "C#"));          // Hello, C#!
 
虚方法需要在导出类(derived class)中重新定义,此处不赘。
 
而在 C# 3.0 中,又引入了一种方法,称为 扩展方法(Extension Method):
 

Extension methods allow an exsting type to be extended with new methods without altering the definition of the original type.

扩展方法是用新方法来扩展原有类型,而不用修改原有类型的定义。
 
扩展原有类型(Extending the existing type)
 
扩展原有类型的机制(手段)有类的继承,还有 C# 中的扩展方法。
 
类的继承通过增加新成员,可以扩展原有类型的数据成员(fields);可以定义新方法或重写被继承类的虚方法来扩展原有类型的方法成员(methods)。但继承机制也存在自己的问题,如果原有类型声明为封闭类型(sealed class),则无法使用继承机制扩展原有类型(如上述的 String class)。再者,导出类(derived class)对基类(base class)的扩展是作用在导出类上的,而非对基类自身的扩展。
 
扩展方法弥补了继承机制的不足。严格说来,扩展方法不是语言本身的机制,而是通过编译程序实现的语法便用设施。不管原有类型是封闭类型还是开放类型,都可以用扩展方法进行扩展。扩展方法只是扩展原有类的方法成员,并不能扩展原有类的数据成员。
 
An extension method is a static method of a static class, where the this modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
扩展方法是静态类的静态方法,其第一个参数要用 this 修饰,而这第一个参数的类型就是要扩展的类型。
 
例子:
 
public static class StringHelper
{
     public static bool IsCapitalized(this string s)
     {
          if (string.IsNullOrEmpty(s)) return false;
          return char.IsUpper(s[0]);
     }
}
 
IsCapitalized 就是定义在静态类上的静态方法,而其第一个参数 string s 用 this 修饰,所以 IsCapitalized 是扩展方法,因为第一个参数的类型是 string,所以 IsCapitalized 扩展的是 string 类型。
 
用法:
 
Console.WriteLine("Perth".IsCapitalized());
 
"Perth".IsCapitalized() 是直接针对 string "Perth" 调用 IsCapitalized(),因为 IsCapitalized() 是 string 的扩展方法,所以这种调用是可以的。
 
扩展方法的调用经过编译器的解析后,就翻译为普通的静态方法调用,即:
 
Console.WriteLine(StringHelper.IsCapitalized("Perth"));
 
而 "Perth".IsCapitalized() 相对于 StringHelper.IsCapitalized("Perth") 而言,更为自然、简洁。
 
所以,扩展方法仍然是一种静态方法,只是 C# 语言引入的语法便用设施而已。编译器的翻译过程如下:
 
     arg0.Method(arg1, arg2, ...);  ==> StaticClass.Method(arg0, arg1, arg2, ...);
 
也就是将扩展方法调用翻译为普通的静态方法调用。
 
扩展方法的应用
 
string 类虽然提供了功能强大的众多方法,但总是不可能满足所有需求。有些经常使用而 string 类又没有提供的功能,可以写成扩展方法,并归入代码库(Library),可以再多个项目中使用。下面是一个例子,两个有关 string 的扩展方法,放入 StringExtensions 类中:
 
    public static class StringExtensions
    {
        public static List<KeyValuePair<string, string>> Listize(this string ss)
        {
            List<KeyValuePair<string, string>> pList = new List<KeyValuePair<string, string>>();
            string[] slist = ss.Split(new char[] { ';' });
            foreach (string s in slist)
            {
                if (string.IsNullOrEmpty(s)) continue;
                string[] pair = s.Split(new char[] { ':' });
                if (pair.Length != 2) continue;
                pList.Add(new KeyValuePair<string, string>(pair[0], pair[1]));
            }
            return pList;
        }
        public static string MatchAndReplace(this string src, string pattern, string dst)
        {
            List<string> mList = new List<string>();
            foreach (Match m in Regex.Matches(src, pattern))
            {
                mList.Add(m.Value);
            }
            foreach (string s in mList)
            {
                if (string.IsNullOrEmpty(s)) continue;
                src = src.Replace(s, dst);
            }
            return src;
        }
    }
 
上述两个扩展方法的用例如下:
 
    string sql = GetSQL();
    string whereTail = "";
    foreach (string k in po.CustomObject.Keys)
    {
        if (string.IsNullOrEmpty(k)) continue;
        if (string.Compare(k, "DemoZoneList", true) == 0)
        {
            whereTail = po.CustomObject[k].Value.Listize().BuildWhereClauseInclusive();
        }
        else if (string.Compare(k, "NonDemoZoneList", true) == 0)
        {
            whereTail = po.CustomObject[k].Value.Listize().BuildWhereClauseExclusive();
        }
    }
    if (!string.IsNullOrEmpty(whereTail))
    {
        sql = sql.MatchAndReplace("1[ ]*=[ ]*1", whereTail);
    }
 
可以看到,string 的扩展方法就像原生方法(指 String 类定义的方法)一样调用,代码看起来更加自然、简洁。
 
扩展方法机制是为了解决 LINQ 的问题而引入的,关于这一点,当另文别述。关于扩展方法的深入讨论,可以参阅《C# in Depth》 Third Edition。
 
 

Extension Methods(扩展方法)的更多相关文章

  1. [C#] Extension Method 扩展方法

    当我们引用第三方的DLL.或者Visual Studio自己的库的时候,或许会发现这样的一个情况,如果这个类型有一个XX的方法就好了.这时候我们可以用到扩展方法,是我们的代码更加灵活和高效. 这里我举 ...

  2. C# Extension Methods(C#类方法扩展)

    使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...

  3. C# -- 扩展方法的应用(Extension Methods)

    当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...

  4. (转)C# -- 扩展方法的应用(Extension Methods)

    本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...

  5. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  6. C# Note21: 扩展方法(Extension Method)及其应用

    前言 今天在开会时提到的一个概念,入职3个多月多注重在项目中使用C#的编程知识,一直没有很认真地过一遍C#的全部语法,当我们新人被问及是否了解Extension Method时,一时之间竟不能很通俗准 ...

  7. [译文]c#扩展方法(Extension Method In C#)

    原文链接: https://www.codeproject.com/Tips/709310/Extension-Method-In-Csharp 介绍 扩展方法是C# 3.0引入的新特性.扩展方法使你 ...

  8. [0] C# 扩展方法(Extension Method)

    有时有这样的情况,有一个类,你不能修改它,但你又想对它扩展(添加一个方法),这个时候就可以用到扩展方法了.请看下面的例子: using System;using System.Collections. ...

  9. c#编程指南(五) 扩展方法(Extension Method)

    C# 3.0就引入的新特性,扩展方法可以很大的增加你代码的优美度,扩展方法提供你扩展.NET Framewoke类的扩展途径,书写和规则也简单的要命. 编写扩展方法有下面几个要求: 第一:扩展方法所在 ...

随机推荐

  1. session 分布式处理-----https://segmentfault.com/a/1190000013447750?utm_source=tag-newest

    第一种:粘性session 粘性Session是指将用户锁定到某一个服务器上,比如上面说的例子,用户第一次请求时,负载均衡器将用户的请求转发到了A服务器上,如果负载均衡器设置了粘性Session的话, ...

  2. [bzoj3289]Mato的文件管理_莫队_树状数组

    Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...

  3. 洛谷—— P1339 [USACO09OCT]热浪Heat Wave

    P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...

  4. Ubuntu 16.04安装FTP客户端filezilla

    1.安装: sudo apt-get install filezilla 参考: http://os.51cto.com/art/201103/247564.htm

  5. C# 运行CMD命令

    /// <summary> /// 运行CMD命令 /// </summary> /// <param name="cmd">命令</pa ...

  6. gem5: 使用ruby memory system中的mesh结构 出现AssertionError错误

    问题:在使用ruby memory system中的mesh结构測试时,出现例如以下错误: Traceback (most recent call last): File "<stri ...

  7. 如何让CMD命令运行后不自动退出

    在命令最后加入一句"cmd /k"

  8. python多线程实现抓取网页

    Python实现抓取网页 以下的Python抓取网页的程序比較0基础.仅仅能抓取第一页的url所属的页面,仅仅要预定URL足够多.保证你抓取的网页是无限级别的哈,以下是代码: ##coding:utf ...

  9. 通过scp批量推送文件到远程目录

    [root@openfire1 script]# cat test.sh  #!/bin/bash   #通过scp推送文件到远程目录 for ip in `cat iplist`  do       ...

  10. Linux I2C设备驱动编写(一)【转】

    本文转载自:http://blog.csdn.net/airk000/article/details/21345457 在Linux驱动中I2C系统中主要包含以下几个成员: I2C adapter 即 ...