當添加程式集引用時,可能會造成某種類型衝突,即你的應用程式在另一個程式集中定義的一個類型有相同的命名,怎麼辦?(關鍵字:C#2.0 、別名、extern alias、::) .

來源:互聯網
上載者:User

http://blog.csdn.net/lovecady/article/details/2562026

 

當添加程式集引用時,可能會造成某種類型衝突,即你的應用程式在另一個程式集中定義的一個類型有相同的命名,那麼你就要為引用添加別名。

當你為程式集添加別名時,在該程式集中使用過的命名空間將在別名下,而不是在global下被解析。

要在一個程式集添加別名,首先在Visual Studio 2005中添加程式集的引用,然後,在解決方案管理器中開啟“引用”檔案夾,顯示引用的程式集屬性。

還需要修改代碼:

[csharp] view plain copy print ?
  1. //由於為程式集引用添加別名,必須在程式的開始添加"extern alias"保留字   
  2.   
  3. extern alias MyClassLibraryAlias;  
  4.   
  5. using System;  
  6.   
  7. using System.Collections.Generic;  
  8.   
  9. using System.Text;  
  10.   
  11. //由於為程式集引用添加別名,所以在使用using時會報錯。   
  12.   
  13. //using Animal;   
  14.   
  15.   
  16.   
  17. namespace program  
  18.   
  19. {  
  20.   
  21.     class Program  
  22.   
  23.     {  
  24.   
  25.         static void Main(string[] args)  
  26.   
  27.         {  
  28.   
  29.             //注意:僅可通過別名來引用   
  30.   
  31.             MyClassLibraryAlias::Animal .Animal.Cow myCow = new MyClassLibraryAlias .Animal .Animal  .Cow();  
  32.   
  33.             MyClassLibraryAlias::Animal.Animal.Chicken myChicken = new MyClassLibraryAlias::Animal.Animal.Chicken();  
  34.   
  35.   
  36.   
  37.             MyClassLibraryAlias::Animal.IMyAnimal animalInterface;  
  38.   
  39.               
  40.   
  41.             animalInterface = myCow;  
  42.   
  43.             animalInterface.EatFood();  
  44.   
  45.               
  46.   
  47.             animalInterface = myChicken;  
  48.   
  49.             animalInterface.EatFood();  
  50.   
  51.               
  52.   
  53.             myCow.EatFood();  
  54.   
  55.   
  56.   
  57.   
  58.   
  59.         }  
  60.   
  61.     }  
  62.   
  63. }  
//由於為程式集引用添加別名,必須在程式的開始添加"extern alias"保留字extern alias MyClassLibraryAlias;using System;using System.Collections.Generic;using System.Text;//由於為程式集引用添加別名,所以在使用using時會報錯。//using Animal;namespace program{    class Program    {        static void Main(string[] args)        {            //注意:僅可通過別名來引用            MyClassLibraryAlias::Animal .Animal.Cow myCow = new MyClassLibraryAlias .Animal .Animal  .Cow();            MyClassLibraryAlias::Animal.Animal.Chicken myChicken = new MyClassLibraryAlias::Animal.Animal.Chicken();            MyClassLibraryAlias::Animal.IMyAnimal animalInterface;                        animalInterface = myCow;            animalInterface.EatFood();                        animalInterface = myChicken;            animalInterface.EatFood();                        myCow.EatFood();        }    }}
[csharp] view plain copy print ?
  1. 使用別名和完整命名空間可能導致過長的程式碼。為了速記,你也可以為完全限定名添加  
使用別名和完整命名空間可能導致過長的程式碼。為了速記,你也可以為完全限定名添加
[csharp] view plain copy print ?
  1. //由於為程式集引用添加別名,必須在程式的開始添加"extern alias"保留字   
  2.   
  3. extern alias MyClassLibraryAlias;  
  4.   
  5. using System;  
  6.   
  7. using System.Collections.Generic;  
  8.   
  9. using System.Text;  
  10.   
  11. //由於為程式集引用添加別名,所以在使用using時會報錯。   
  12.   
  13. //using Animal;   
  14.   
  15. //使用別名和完整命名空間可能導致過長的程式碼。為了速記,你也可以為完全限定名添加別名   
  16.   
  17. using MyLibrary = MyClassLibraryAlias::Animal;  
  18.   
  19.   
  20.   
  21. namespace program  
  22.   
  23. {  
  24.   
  25.     class Program  
  26.   
  27.     {  
  28.   
  29.         static void Main(string[] args)  
  30.   
  31.         {  
  32.   
  33.             //注意:僅可通過別名來引用   
  34.   
  35.             MyClassLibraryAlias::Animal .Animal.Cow myCow = new MyClassLibraryAlias .Animal .Animal  .Cow();  
  36.   
  37.               
  38.   
  39.             //為完全限定名添加別名後   
  40.   
  41.             MyLibrary.Animal.Chicken myChicken = new MyLibrary.Animal.Chicken();  
  42.   
  43.   
  44.   
  45.             MyClassLibraryAlias::Animal.IMyAnimal animalInterface;  
  46.   
  47.               
  48.   
  49.             animalInterface = myCow;  
  50.   
  51.             animalInterface.EatFood();  
  52.   
  53.               
  54.   
  55.             animalInterface = myChicken;  
  56.   
  57.             animalInterface.EatFood();  
  58.   
  59.               
  60.   
  61.             myCow.EatFood();  
  62.   
  63.   
  64.   
  65.   
  66.   
  67.         }  
  68.   
  69.     }  
  70.   
  71. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.