Http://blog.csdn.net/lovecady/article/details/2562026
When an assembly reference is added, a type conflict may occur. That is, if the type defined by your application in another assembly has the same name, you must add an alias for the reference.
When you add an alias to an assembly, the namespace used in the assembly will be resolved under the alias instead of global.
To add an alias to an assembly, first add an Assembly reference in Visual Studio 2005, and then open the reference folder in solution manager to display the referenced assembly attributes.
You also need to modify the code:
[CSHARP]
View plain
Copy
Print
?
- // Because an alias is added for the Assembly reference, you must add the "extern alias" reserved word at the beginning of the program
- Extern alias myclasslibraryalias;
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- // An error is reported when using is used because an alias is added for the Assembly reference.
- // Using animal;
- Namespace Program
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- // Note: Only alias can be used for reference.
- 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 ();
- }
- }
- }
// Because an alias is added for the Assembly reference, the "extern alias" reserved word extern alias myclasslibraryalias; using system must be added at the beginning of the program. collections. generic; using system. text; // an error is reported when using is used because an alias is added for the Assembly reference. // Using animal; namespace program {class program {static void main (string [] ARGs) {// Note: You can reference 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
?
- Using aliases and fully qualified namespaces may lead to too long code lines. For stenography, you can also add a fully qualified name
Using aliases and fully qualified namespaces may lead to too long code lines. For stenography, you can also add a fully qualified name
[CSHARP]
View plain
Copy
Print
?
- // Because an alias is added for the Assembly reference, you must add the "extern alias" reserved word at the beginning of the program
- Extern alias myclasslibraryalias;
- Using system;
- Using system. Collections. Generic;
- Using system. text;
- // An error is reported when using is used because an alias is added for the Assembly reference.
- // Using animal;
- // Using aliases and fully qualified namespaces may lead to too long code lines. For stenography, you can also add aliases for fully qualified names.
- Using mylibrary = myclasslibraryalias: animal;
- Namespace Program
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- // Note: Only alias can be used for reference.
- Myclasslibraryalias: Animal. Animal. Cow mycow = new myclasslibraryalias. Animal. Animal. Cow ();
- // Add an alias for a fully qualified name
- Mylibrary. Animal. Chicken mychicken = new mylibrary. Animal. Chicken ();
- Myclasslibraryalias: Animal. imyanimal animalinterface;
- Animalinterface = mycow;
- Animalinterface. eatfood ();
- Animalinterface = mychicken;
- Animalinterface. eatfood ();
- Mycow. eatfood ();
- }
- }
- }