Referring to the answer by Jon Skeet here: Pass An Instantiated System.Type as a Type Parameter for a Generic Class
I need to load a Generic type based on the name of the generic type, and the name of the type that is the type parameter for the generic. So from Jon's example I would have:
string genName = "MyNamespace.Generic";
string itemName = "System.String";
I have the following code that will load a type based on the name of the type and a fully justified assembly name/path. It works fine for "simple types"
public Type GetTypeOf(string assemblyPath, string className)
{
var asmbly = System.Reflection.Assembly.LoadFrom(assemblyPath); //open assembly
return asmbly.GetType(className, true, true); //throws error, not case sensitive
}
I was hoping to use this as follows:
//Get the types
var genTyp = GetTypeOf(genPath,genName);
var itemTyp = GetTypeOf(itemPath,itemName);
//Put them together:
var typ = getType.MakeGenericType(itemTyp);
This falls over on the first line with a System.TypeLoadException
stating:
Could not load type <TypeName here> from assembly <AssemblyName here>
I've tried a number of permutations of creating the generic, included supplying the full class name MyNamespace.Generic<System.String>
. It works correctly when I specify a non-generic type to load from the same assembly that contains the generic type.