在 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。
 
 

最新文章

  1. myBatis中 collection 或 association 联合查询 中column 传入多个参数值
  2. 理解 Nova 架构 - 每天5分钟玩转 OpenStack(23)
  3. Synchronized
  4. SQL判断某列中是否包含中文字符、英文字符、纯数字 (转)
  5. Android Animation学习(六) View Animation介绍
  6. 表视图控制器(TableViewController)(一)
  7. 100天成就卓越领导力:新晋领导者的First100训练法
  8. 【Effective c++】条款6:若不想使用编译器自动生成的函数就应该明确拒绝
  9. Entity FrameWork 指导文章
  10. 小白的Python之路 day1 数据类型,数据运算
  11. javap反编译命令详解&amp;Eclipse中配置javap命令
  12. 在 root 下执行 Oracle 程序时找不到 libclntsh.so.11.1 错误的解决办法。
  13. Java使用RabbitMQ之订阅分发(Topic)
  14. 【php增删改查实例】第十二节 - 数据删除功能
  15. global 全局变量 nonlocal 局部变量
  16. base标签对svg的影响
  17. java byte[]与十六进制字符串相互转换
  18. js的日期格式判断
  19. Java Nashorn--Part 2
  20. Android 自动化测试

热门文章

  1. 调试pcb板子的步骤
  2. 关于datanode多磁盘存储策略
  3. free web rich code eidtor
  4. Linux command2
  5. PatentTips – GPU Saving and Restoring Thread Group Operating State
  6. Ubuntu下常规方法安装软件
  7. Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)
  8. SiteMesh2-sitemesh.xml的ConfigDecoratorMapper映射器的用法
  9. Quartz.Net 使用心得(二)
  10. 非常适合新手的jq/zepto源码分析02