title: Is it possible to dynamically specify an extern alias in C#?
body: <p>Let's say you have two DLLs which contain virtually identical code. There are only minor differences between them, but those differences are important enough that need to remain distinct libraries. Actually, in my particular case, they are third-party Java libraries which have been converted to .NET DLLs using IKVM, so we cannot collapse them into one without rewriting the code ourselves. These represent slightly different versions of a server API provided by an outside supplier.</p>
  
  <p>When we go to use these libraries within our code, we are forced to write two classes which are exactly the same, except that the external alias we specify is different.</p>
  
  <p>For example:</p>
  
  <pre><code>extern alias Lib1;
  
  namespace MyNamespace
  {
      public Class1
      {
          private Lib1::A a = new Lib1::A();
  
          public Class1()
          {
              this.a.DoStuff();
          }
  
          // Uses Lib1 extern alias throughout
      }
  }
  
  extern alias Lib2;
  
  namespace MyNamespace
  {
      public Class2
      {
          private Lib2::A a = new Lib2::A();
  
          public Class1()
          {
              this.a.DoStuff();
          }
  
          // Totally identical to Class1, except that it uses Lib2 extern alias throughout
      }
  }
  </code></pre>
  
  <p>All the code in Class1 is exactly the same as Class2. Just the alias is different.</p>
  
  <p>When we want to update the code, we are forced to duplicate the code from one class into all the others. Then we simply replace one alias with another.</p>
  
  <p>Is there a way in which we could create a single class, and simply specify dynamically which extern alias to use when we instantiate the class?</p>
  
  <p>Thanks for your help.</p>