Say I have this little bit of code:
public static void LoadSomething(Type t)
{
var t1 = Type.GetType(t.AssemblyQualifiedName);
var t2 = t
.Assembly
.GetTypes()
.First(ta => ta.AssemblyQualifiedName == t.AssemblyQualifiedName);
}
What happens is that t1 is null and t2 is not null. I was confused since if I call it like so…
LoadSomething(typeof(SomeObject));
then neither are null but what I am actually doing is more like this (not really, this is massively simplified but it illustrates my point):
LoadSomething(Assembly.LoadFile(@"C:\....dll").GetTypes().First());
So the first part of my question (for my information) is…
In the second case, since the assembly must be loaded up and I found the type out of it, why does Type.GetType return null?
And secondly (to actually solve my problem)…
Is there some other way that I could load a type when I only have the assembly qualified name as a string (that I know has been previously loaded by using the Assembly.Load methods)?
Yes. There is a
GetTypeoverload that allows that. It takes an “assembly resolver” function as parameter: