I thought I’d write up a question I posed today on StackOverflow here: Loading a generic type using Assembly.LoadFrom
I had a pre-existing method that I use to get a type based on a full assembly path and a class name. This resides in a Utils
namespace.
I was hoping to use this as follows:
This falls over on the first line with a System.TypeLoadException
stating:
Could not load type from assembly
I tried a number of permutations of the Generic and Item types, for example MyNamespace.Generic<System.String>
Solution
The answer, as suggested by Carsten on the SO question, is that the item type needs to use the fully qualified name to be loaded.
The example Carsten gave was in this format:
Using this template I setup a different version of GetTypeOf
, which I named GetGenericTypeOf
just to make its purpose clear:
This new function is then applied in conjunction with the original to get all the information required to create an instance of the generic type: