分享

C#之扩展方法学习

 kittywei 2012-02-17
  1. 扩展方法基础:

当方法的第一个形参包含 this 修饰符时称该方法为扩展方法 (extension method)。只能在非泛型、非嵌套静态类中声明扩展方法。扩展方法的第一个形参不能带有除 this 之外的其他修饰符,而且形参类型不能是指针类型。

下面是一个声明两个扩展方法的静态类的示例


 1public static class Extensions
 2{
 3        public static int ToInt32(this string s) {
 4            return Int32.Parse(s);
 5        }

 6        public static T[] Slice<T>(this T[] source, int index, int count) {
 7            if (index < 0 || count < 0 || source.Length – index < count)
 8                throw new ArgumentException();
 9            T[] result = new T[count];
10            Array.Copy(source, index, result, 0, count);
11            return result;
12        }

13}

14

扩展方法是常规静态方法。另外,如果它的包容静态类在范围之内,则可以使用实例方法调用语法来调用扩展方法,同时将接收器表达式用作第一个实参。
下面的程序使用上面声明的扩展方法


 1static class Program
 2{
 3        static void Main() {
 4            string[] strings = "1""22""333""4444" };
 5            foreach (string s in strings.Slice(12)) {
 6                Console.WriteLine(s.ToInt32());
 7            }

 8        }

 9}

10

Slice 方法在 string[] 上可用ToInt32 方法在字符串上可用原因是它们都已声明为扩展方法。该程序的含义与下面使用普通静态方法调用的程序相同:


 1static class Program
 2{
 3        static void Main() {
 4            string[] strings = "1""22""333""4444" };
 5            foreach (string s in Extensions.Slice(strings, 12)) {
 6                Console.WriteLine(Extensions.ToInt32(s));
 7            }

 8        }

 9}

10


2. 扩展方法调用:
如果在正常的方法调用处理找不到适用的方法,则将尝试以扩展方法调用的形式处理该构造。目标是查找最佳的类型C,以便可以进行相应的静态方法调用:
如果满足以下各项,则扩展方法 C.M 符合条件:

·         C 为非泛型、非嵌套类

·         M 的名称为和用户调用方法名称相同

·         M 作为静态方法应用于参数时是可访问且适用的

存在从表倒是到方法M 的第一个参数的类型的隐式标识、引用或装箱转换。
对类型C的搜索操作如下:

·         从最接近的封闭命名空间声明开始,接下来是每个封闭命名空间声明,最后是包含编译单元,搜索将连续进行以找到候选的扩展方法集:

o        如果定的命名空编译单元直接包含具有适当扩展方法M的非泛型C则这展方法的集合集。

   o        如果使用定命名空编译单元中的命名空指令入的命名空直接包含具有适当扩展方法M的非泛型C则这展方法的集合集。

·         如果在任何封闭命名空间声明或编译单元中都找不到候选集,则会出现编译时错误。

·         否则,将对候选集应用重载决策。如果找不到一个最佳方法,则会出现编译时错误。

·         C是将最佳方法声明为扩展方法的类型。

·       如果将C用作目标,则将以静态方法调用的形式处理该方法调用。

上述规则表示,实例方法优先于扩展方法,内部命名空间声明中可用的扩展方法优先于外部命名空间声明中可用的扩展方法,并且直接在命名空间中声明的扩展方法优先于通过 using 命名空间指令导入该命名空间的扩展方法。例如:


 1public static class E
 2{
 3        public static void F(this object obj, int i) { }
 4        public static void F(this object obj, string s) { }
 5}

 6class A { }
 7class B
 8{
 9        public void F(int i) { }
10}

11class C
12{
13        public void F(object obj) { }
14}

15class X
16{
17        static void Test(A a, B b, C c) {
18            a.F(1);                // E.F(object, int)
19            a.F("hello");        // E.F(object, string)
20            b.F(1);                // B.F(int)
21            b.F("hello");        // E.F(object, string)
22            c.F(1);                // C.F(object)
23            c.F("hello");        // C.F(object)
24        }

25}

26

在该示例中,B 的方法优先于第一个扩展方法,而 C 的方法优先于这两个扩展方法。
下面是另一个示例:


 1public static class C
 2{
 3        public static void F(this int i) { Console.WriteLine("C.F({0})", i); }
 4        public static void G(this int i) { Console.WriteLine("C.G({0})", i); }
 5        public static void H(this int i) { Console.WriteLine("C.H({0})", i); }
 6}

 7namespace N1
 8{
 9        public static class D
10        {
11            public static void F(this int i) { Console.WriteLine("D.F({0})", i); }
12            public static void G(this int i) { Console.WriteLine("D.G({0})", i); }
13        }

14}

15namespace N2
16{
17        using N1;
18        public static class E
19        {
20            public static void F(this int i) { Console.WriteLine("E.F({0})", i); }
21        }

22        class Test
23        {
24            static void Main(string[] args)
25            {
26                1.F();
27                2.G();
28                3.H();
29            }

30        }

31}

32

该示例的输出为:

E.F(1)
D.G(2)
C.H(3)

D.G 优先于 C.G,而 E.F 优先于 D.F C.F

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多