- 扩展方法基础:
当方法的第一个形参包含 this 修饰符时,称该方法为扩展方法 (extension method)。只能在非泛型、非嵌套静态类中声明扩展方法。扩展方法的第一个形参不能带有除 this 之外的其他修饰符,而且形参类型不能是指针类型。
下面是一个声明两个扩展方法的静态类的示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public static class Extensions
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static int ToInt32(this string s)
{
4
return Int32.Parse(s);
5
}
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
扩展方法是常规静态方法。另外,如果它的包容静态类在范围之内,则可以使用实例方法调用语法来调用扩展方法,同时将接收器表达式用作第一个实参。
下面的程序使用上面声明的扩展方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
static class Program
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static void Main()
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] strings =
{ "1", "22", "333", "4444" };
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (string s in strings.Slice(1, 2))
{
6
Console.WriteLine(s.ToInt32());
7
}
8
}
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Slice 方法在 string[] 上可用,ToInt32 方法在字符串上可用,原因是它们都已声明为扩展方法。该程序的含义与下面使用普通静态方法调用的程序相同:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
static class Program
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static void Main()
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] strings =
{ "1", "22", "333", "4444" };
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (string s in Extensions.Slice(strings, 1, 2))
{
6
Console.WriteLine(Extensions.ToInt32(s));
7
}
8
}
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 扩展方法调用:
如果在正常的方法调用处理找不到适用的方法,则将尝试以扩展方法调用的形式处理该构造。目标是查找最佳的类型C,以便可以进行相应的静态方法调用:
如果满足以下各项,则扩展方法 C.M 符合条件:
· C 为非泛型、非嵌套类
· M 的名称为和用户调用方法名称相同
· M 作为静态方法应用于参数时是可访问且适用的
存在从表倒是到方法M 的第一个参数的类型的隐式标识、引用或装箱转换。
对类型C的搜索操作如下:
· 从最接近的封闭命名空间声明开始,接下来是每个封闭命名空间声明,最后是包含编译单元,搜索将连续进行以找到候选的扩展方法集:
o 如果给定的命名空间或编译单元直接包含具有适当扩展方法M的非泛型类型声明C,则这些扩展方法的集合为候选集。
o 如果使用给定命名空间或编译单元中的命名空间指令导入的命名空间直接包含具有适当扩展方法M的非泛型类型声明C,则这些扩展方法的集合为候选集。
· 如果在任何封闭命名空间声明或编译单元中都找不到候选集,则会出现编译时错误。
· 否则,将对候选集应用重载决策。如果找不到一个最佳方法,则会出现编译时错误。
· C是将最佳方法声明为扩展方法的类型。
· 如果将C用作目标,则将以静态方法调用的形式处理该方法调用。
上述规则表示,实例方法优先于扩展方法,内部命名空间声明中可用的扩展方法优先于外部命名空间声明中可用的扩展方法,并且直接在命名空间中声明的扩展方法优先于通过 using 命名空间指令导入该命名空间的扩展方法。例如:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public static class E
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void F(this object obj, int i)
{ }
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void F(this object obj, string s)
{ }
5
}
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
class A
{ }
7
class B
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void F(int i)
{ }
10
}
11
class C
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void F(object obj)
{ }
14
}
15
class X
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在该示例中,B 的方法优先于第一个扩展方法,而 C 的方法优先于这两个扩展方法。
下面是另一个示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public static class C
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void F(this int i)
{ Console.WriteLine("C.F({0})", i); }
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void G(this int i)
{ Console.WriteLine("C.G({0})", i); }
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void H(this int i)
{ Console.WriteLine("C.H({0})", i); }
6
}
7
namespace N1
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public static class D
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void F(this int i)
{ Console.WriteLine("D.F({0})", i); }
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void G(this int i)
{ Console.WriteLine("D.G({0})", i); }
13
}
14
}
15
namespace N2
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
17
using N1;
18
public static class E
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void F(this int i)
{ Console.WriteLine("E.F({0})", i); }
21
}
22
class Test
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
static void Main(string[] args)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
1.F();
27
2.G();
28
3.H();
29
}
30
}
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
该示例的输出为:
E.F(1)
D.G(2)
C.H(3)
D.G 优先于 C.G,而 E.F 优先于 D.F 和 C.F。