title: COM interop assembly loading sequence
body: <p>There is very strange assembly reference problem and loading problem I experience with my Outlook addin. Here are the detail (a long story :) ):</p>
  
  <p>I have an old Outlook addin, written and build using .Net 1.1. The addin is loaded using an unmanaged shim in its own application domain. It works OK, with .Net 2.0, even if 1.1 is not present on the user's machine.</p>
  
  <p>The addin uses a custom Outlook interop assembly, created by VS 2003 against Outlook 2000, and after that rebuild to be strongly-named (as is my addin).</p>
  
  <p>In the addin project I reference only this custom interop assembly, no reference to the official MS interop assemblies.</p>
  
  <p>When this addin is used in an environment with Outlook 2007 and .Net 2.0, where the official MS interop assemblies are installed in GAC, for some reason I see that the addin loads and uses them.</p>
  
  <p>In the code of the Connect class, I have a using directive:</p>
  
  <pre><code>using Outlook;
  </code></pre>
  
  <p>which is the namespace of my custom interop assembly.</p>
  
  <p>In Connect ctor I have these lines of code (added for testing purposes):</p>
  
  <pre><code>Assembly.LoadFrom(PATHTOMYASSEMBLY + "Interop.Outlook.dll");
  Type type = typeof(Outlook.ApplicationClass);
  logger.Debug("Outlook.Application full type is: {0}", type.AssemblyQualifiedName);
  </code></pre>
  
  <p>This outputs:</p>
  
  <blockquote>
    <p>Outlook.Application full type is:
    Outlook.ApplicationClass,
    Interop.Outlook, Version=9.0.0.0,
    Culture=neutral,
    PublicKeyToken=4cfbdc5349cf59d8</p>
  </blockquote>
  
  <p>Which is exactly what I would expected.</p>
  
  <p>The problem is, that when the 
  OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) 
  is called, I see in the log (I have a hook to AssemblyLoad event of the current domain) that MS interop assembly is loaded as well:</p>
  
  <pre><code>private void app_domain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
  {
      Assembly loadedAssembly = args.LoadedAssembly;
      logger.Debug("Assembly {0} is loaded from: {1}", loadedAssembly.FullName, loadedAssembly.GlobalAssemblyCache ? "GAC" : loadedAssembly.Location);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>Assembly
    Microsoft.Office.Interop.Outlook,
    Version=12.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c is
    loaded from: GAC</p>
  </blockquote>
  
  <p>My OnConnection method starts like this:</p>
  
  <pre><code>public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
  {
      Type type = application.GetType();
      logger.Debug("OnConnection application object's full type is: {0}", type.AssemblyQualifiedName);
  
      Outlook.Application applicationObject = (Outlook.Application)application;
  </code></pre>
  
  <p>This outputs:</p>
  
  <blockquote>
    <p>OnConnection application object's full
    type is:
    Microsoft.Office.Interop.Outlook.ApplicationClass,
    Microsoft.Office.Interop.Outlook,
    Version=12.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c</p>
  </blockquote>
  
  <p>This is really weird, as you can see that on the next line I can successfully cast to Outlook.Application without any problems.</p>
  
  <p>I have checked with Reflector, and my assembly DOES NOT reference Microsoft's interop assemblies in any way. Same for my Interop.Outlook.dll.</p>
  
  <p>So, does someone knows what's going on? What is the answer for these questions:</p>
  
  <ol>
  <li><p>Why it loads the Microsoft assemblies at all?</p></li>
  <li><p>How it is possible to cast between unrelated classes/interfaces, defined in different assemblies?</p></li>
  </ol>
  
  <p>NOTE: I have created a new addin, very simple one, which does nothing, just loads. I could reproduce the problem, so really, does anyone knows how CLR decides what interop to load and from where. Besides in GAC, is there another place (registry???) where there is a link between a COM object and the interop it requires?</p>