当添加程序集引用时,可能会造成某种类型冲突,即你的应用程序在另一个程序集中定义的一个类型有相同的命名,怎么办?(关键字:C#2.0 、别名、extern alias、::) .

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

 

当添加程序集引用时,可能会造成某种类型冲突,即你的应用程序在另一个程序集中定义的一个类型有相同的命名,那么你就要为引用添加别名。

当你为程序集添加别名时,在该程序集中使用过的命名空间将在别名下,而不是在global下被解析。

要在一个程序集添加别名,首先在Visual Studio 2005中添加程序集的引用,然后,在解决方案管理器中打开“引用”文件夹,显示引用的程序集属性。

还需要修改代码:

  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();





        }

    }

}
  1. 使用别名和完全限定的命名空间可能导致过长的代码行。为了速记,你也可以为完全限定名添加  
使用别名和完全限定的命名空间可能导致过长的代码行。为了速记,你也可以为完全限定名添加

  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. }  
posted @ 2012-11-13 10:13  China Soft  阅读(640)  评论(1编辑  收藏  举报