擴展方法是一個雙刃劍,你如果要用擴展方法,就一定得準備碰到別人也用擴展方法,并且萬一出現你們都對同一個類進行擴展函數名,參數一樣的擴展時候, 會出的各種問題.
?
說明:以下測試是基于Orcas Beta1環境的測試,VS2008 正式版發布后,也許會發生變化。
?
如果你對擴展方法不是很熟悉,建議先看我之前的兩篇博客:
C#3.0 中的擴展方法 (Extension Methods)
?
如果我們項目中,在不經意期間,出現了重名的擴展方法,這時候編譯器會如何處理呢?這就是本文要探討的問題。
?
先說結論:編譯器按照下述三個優先級順序去尋找到底哪個方法被調用到:
假設,我們是在A 命名空間下,調用 類?B 的?C 方法。 類的 C 方法可能是在 D 命名空間下使用擴展方法擴展實現的。
?
1、類?B 中是否真實存在 C 方法(非擴展方法),如果存在,則調用之,存在時,不考慮是否有擴展,直接進行調用。(這種情況請看我之前的博客: C#3.0 中的擴展方法 (Extension Methods) )
2、命名空間 A ,是否存在對C方法的擴展,如果存在,則調用這種情況下的擴展;看示例代碼二
3、調用代碼中,除了 命名空間 A,還引用了其他命名空間 D和 F。A命名空間沒有對C方法進行擴展,則調用D命名空間下的擴展。
?
特殊情況:
如果 F 和 D 命名空間都對 C 方法進行了擴展,則編譯器報錯誤;看下面的示例代碼一
如果 D 和 A命名空間都對 C 方法進行了擴展,則調用 A命名空間下的擴展,編譯器不會報錯;看示例代碼二
?
示例代碼一:
以下代碼會編譯時報錯:
The call is ambiguous between the following methods or properties: 'ConsoleApplication1.MyClass01.DO(int)' and 'ConsoleApplication2.MyClass02.DO(int)
表明???? using ConsoleApplication2;???? using ConsoleApplication1;? 導致 ConsoleApplication2 和 ConsoleApplication1? 命名空間同級,同級搜索出現重名,報錯。
namespace ConsoleApplication2
{
??? using System;
??? static class MyClass02
??? {
??????? internal static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("ConsoleApplication2.MyClass02.DO {0}", i));
??????? }
??? }
}
namespace ConsoleApplication1
{
??? using System;
??? static class MyClass01
??? {
??????? public static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("HongJunGuo.MyClass01.DO {0}", i));
??????? }
??? }
}
namespace HongJunGuo.Test
{
??? using System;
??? using ConsoleApplication2;
??? using ConsoleApplication1;
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? int w = 9;
??????????? w.DO();
??????????? Console.ReadLine();
??????? }
??? }
}
?
示例代碼二
以下代碼返回的信息為 HongJunGuo.MyClass01.DO 9, 標明優先找 本命名空間下的擴展方法。
namespace ConsoleApplication2
{
??? using System;
??? static class MyClass02
??? {
??????? internal static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("ConsoleApplication2.MyClass02.DO {0}", i));
??????? }
??? }
}
namespace HongJunGuo.Test
{
??? using System;
??? using ConsoleApplication2;
??? static class MyClass01
??? {
??????? public static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("HongJunGuo.MyClass01.DO {0}", i));
??????? }
??? }
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? int w = 9;
??????????? w.DO();
??????????? Console.ReadLine();
??????? }
??? }
}
?
特殊情況:如果出現下面的代碼:
編譯器并不會報錯,返回的信息是 ConsoleApplication2.MyClass02.DO , ConsoleApplication2.MyClass02.DO MyObject
原因: 上級命名空間在處理邏輯上跟其他命名空間處理邏輯是一樣的.
namespace ConsoleApplication2
{
??? using System;
??? using HongJunGuo.Test;
??? static class MyClass02
??? {
??????? public static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("ConsoleApplication2.MyClass02.DO {0}", i));
??????? }
??????? public static void DO(this MyObject obj)
??????? {
??????????? Console.WriteLine("ConsoleApplication2.MyClass02.DO MyObject");
??????? }
??? }
}
namespace HongJunGuo
{
??? using System;
??? using HongJunGuo.Test;
??? static class MyClass01
??? {
??????? public static void DO(this int i)
??????? {
??????????? Console.WriteLine(string.Format("HongJunGuo.MyClass01.DO {0}", i));
??????? }
??????? public static void DO(this MyObject obj)
??????? {
??????????? Console.WriteLine("HongJunGuo.MyClass01.DO MyObject");
??????? }
??? }
}
namespace HongJunGuo.Test
{
??? using System;
??? using ConsoleApplication2;
??? public class MyObject
??? {
??????? public MyObject()
??????? {
??????? }
??? }
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? int w = 9;
??????????? w.DO();
??????????? MyObject obj = new MyObject();
??????????? obj.DO();
??????????? Console.ReadLine();
??????? }
??? }
}
?
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1649494
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
