When an assembly reference is added, a type conflict may occur, that is, the type defined by your application in another assembly has the same name. What should I do? (Keyword: C #2.0, alias, extern alias ,::).

Source: Internet
Author: User

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 ?
  1. // Because an alias is added for the Assembly reference, you must add the "extern alias" reserved word at the beginning of the program
  2. Extern alias myclasslibraryalias;
  3. Using system;
  4. Using system. Collections. Generic;
  5. Using system. text;
  6. // An error is reported when using is used because an alias is added for the Assembly reference.
  7. // Using animal;
  8. Namespace Program
  9. {
  10. Class Program
  11. {
  12. Static void main (string [] ARGs)
  13. {
  14. // Note: Only alias can be used for reference.
  15. Myclasslibraryalias: Animal. Animal. Cow mycow = new myclasslibraryalias. Animal. Animal. Cow ();
  16. Myclasslibraryalias: Animal. Animal. Chicken mychicken = new myclasslibraryalias: Animal. Animal. Chicken ();
  17. Myclasslibraryalias: Animal. imyanimal animalinterface;
  18. Animalinterface = mycow;
  19. Animalinterface. eatfood ();
  20. Animalinterface = mychicken;
  21. Animalinterface. eatfood ();
  22. Mycow. eatfood ();
  23. }
  24. }
  25. }
// 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 ?
  1. 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 ?
  1. // Because an alias is added for the Assembly reference, you must add the "extern alias" reserved word at the beginning of the program
  2. Extern alias myclasslibraryalias;
  3. Using system;
  4. Using system. Collections. Generic;
  5. Using system. text;
  6. // An error is reported when using is used because an alias is added for the Assembly reference.
  7. // Using animal;
  8. // Using aliases and fully qualified namespaces may lead to too long code lines. For stenography, you can also add aliases for fully qualified names.
  9. Using mylibrary = myclasslibraryalias: animal;
  10. Namespace Program
  11. {
  12. Class Program
  13. {
  14. Static void main (string [] ARGs)
  15. {
  16. // Note: Only alias can be used for reference.
  17. Myclasslibraryalias: Animal. Animal. Cow mycow = new myclasslibraryalias. Animal. Animal. Cow ();
  18. // Add an alias for a fully qualified name
  19. Mylibrary. Animal. Chicken mychicken = new mylibrary. Animal. Chicken ();
  20. Myclasslibraryalias: Animal. imyanimal animalinterface;
  21. Animalinterface = mycow;
  22. Animalinterface. eatfood ();
  23. Animalinterface = mychicken;
  24. Animalinterface. eatfood ();
  25. Mycow. eatfood ();
  26. }
  27. }
  28. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.