title: How can I get a type from a referenced assembly via reflection
body: <p>Suppose I have a factory method, which wants to construct an instance of a type chosen at run-time via reflection. Suppose further that my factory method is generic code that doesn't directly reference the assembly that contains the specified type, though it will be run from within an application that has the necessary assembly referenced.</p>
  
  <p>How do I go about writing code that can find this type? If I do the following</p>
  
  <pre><code>public object CreateInstance(string typeName)
  {
      Type desiredType = Assembly.GetExecutingAssembly().GetType(typename);
  
      // Instantiate the type...
  }
  </code></pre>
  
  <p>this appears to fail because the type isn't defined in the executing assembly. If I could get all the assemblies available at runtime, I could iterate over them and find which one contains the type I want. But I can't see a way to do that. <code>AppDomain.CurrentDomain.GetAssemblies()</code> looks promising, but doesn't return all assemblies that I've referenced in my project.</p>
  
  <p><b>Edit</b>: Several people have pointed out that I need to load the assembly. The trouble is, this piece of code doesn't know which assembly it should load, since I'm attempting to write this code in such a way that it does not depend on the other assemblies.</p>
  
  <p>I deliberately left out the details of <code>typeName</code>, since the mapping from string to type is actually more complicated in my real code. In fact, the type is identified by a custom attribute that contains the specified string, but if I can get hold of a list of types, I don't have a problem with restricting the list to the desired type.</p>