How to add multiple versions of the assembly without causing conflicts! -WP

An unusual sticky situation it is!

You have a external class, say Class Saviour , in your project. Class Saviour  uses another class, Class MostSoughtAfter from codeplex. Class Saviour has implemented Class MostSoughtAfter to suit its needs. So where ever you create an instance of Class MostSoughtAfter,  it will refer the Class MostSoughtAfter defined by Class Saviour . Suppose you need to use a function in the original Class MostSoughtAfter, and you add the dll . Visual Studio will throw a warning saying there is an ambiguity and that Class MostSoughtAfter implemented in Class Saviour is used to instantiate.

Bingo! You have multiple versions of the same class in your project and you need to reference them separately. I found a similar question in StackOverflow

Now in C# we have an “extern alias” keyword which will allow us to do the same. MSDN says

By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, which enables them to be used in the same file.

  1. In the solution explorer of your project, click the properties of the dll you want to reference,
  2. The Aliases field is set to ‘global’ by default. Change the global to name of your choice, say MyDLL.
  3. In the class you want to use the dll, first declare the alias right above the using statements
    extern alias MyDLL;
    
  4. To use a method in the assembly we make use of :: qualifier
    MyDLL:: System.Windows.Media.Imaging.MyClass.Resize();
    
  5. Thus a simple creation of an instance of MyClass will use the class implemented in the project. And if you want to use the original dll reference using the alias .

‘extern’  and an MSDN blog saves my day!!

References

Happy coding !! 😀

Advertisements

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