How to Reference or use same fully qualified class name from different assemblies


Two or more different assemblies can contain a type with a same name and same namespace.
What will happen if you reference those assemblies in your project?
Well, referencing won’t result into any error. 🙂
But.
How about making an object of that class?
This will fail. Compilation error will be generated because of ambiguity. Which type from which assembly should be used?
Compiler can’t make this decision on it’s own and rather i would say, it should not.
This should be done by a programmer/coder to clear his intentions to the compiler.
But How?

Solution

    Let’s say, we have two assemblies:

  • Assembly1
  • Assembly2

Both assemblies define a type, say class having fully qualified name as SomeNameSpace.SomeClassName

We have a project where we refer the above two assemblies.
In order to instantiate an object of ProjectLibrary.Class defined in Assembly1, do:

  1. Change the Aliases property of one of the references
  2. Whenever any reference is added, Aliases is set to global. Change this to your desired name, say Assembly1
    Though this can be done for both but doing it for one will automatically open doors for another assembly.

  3. Write a code statement on the top of your file: extern alias Assembly1;
  4. Lastly, change the object instantiation statement to
    Assembly1::ProjectLibrary.Class obj = new Assembly1::ProjectLibrary.Class();
  5. “::” is a Namespace Alias Qualifier in .Net.

That’s it.

Advertisements

About Sunil Singhal

A human being whose dreams are tied to a Horse that will never tire
This entry was posted in Common Technical Problems and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s