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.
How do I go about writing code that can find this type? If I do the following
public object CreateInstance(string typeName)
{
Type desiredType = Assembly.GetExecutingAssembly().GetType(typename);
// Instantiate the type...
}
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. AppDomain.CurrentDomain.GetAssemblies()
looks promising, but doesn't return all assemblies that I've referenced in my project.
Edit: 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.
I deliberately left out the details of typeName
, 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.