mardi 17 mai 2016

Type.GetType return null even when using fully qualified type name

In the following code fragment, Type.GetType on the final line sometimes returns null.

string assemblyName = "My.AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
string typeName = "My.Namespaced.ClassName";
Type t1 = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(x => x.GetTypes())
        .FirstOrDefault(x => x.FullName == TypeName &&
                             x.Assembly.FullName == AssemblyName);
string qualifiedName = t1.AssemblyQualifiedName; // this result looks sane
Type t2 = Type.GetType(qualifiedName); // returns null

As you can see, I'm using the fully qualified name returned by Type.AssemblyQualifiedName, and as expected, the value of qualifiedName is My.Namespaced.ClassName, My.AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.

At some point in my callstack, Type.GetType returns the expected type, but then, if we make a call into another assembly, the exact same snippet of code results in t1 finding the type, but t2 being null.

I see StackOverflow is littered with duplicate questions when people have not used the assembly qualified name, but that's not the case here.





Aucun commentaire:

Enregistrer un commentaire