I have come across a strange behaviour in my (huge) .NET 4 project. At some point in the code, I am referring to a fully qualified type, say:
System.Type type = typeof (Foo.Bar.Xyz);
later on, I do this:
System.Type type = System.Type.GetType ("Foo.Bar.Xyz");
and I get back null. I cannot make sense of why this is happening, because my type name is correct, and I have checked with other types and they get resolved properly. Moreover, the following LINQ query finds the type:
var types = from assembly in System.AppDomain.CurrentDomain.GetAssemblies ()
from assemblyType in assembly.GetTypes ()
where assemblyType.FullName == typeName
select assemblyType;
System.Type type = types.FirstOrDefault ();
Are there any reasons why System.Type.GetType could fail?
I have finally had to resort to this piece of code instead of GetType:
System.Type MyGetType(string typeName)
{
System.Type type = System.Type.GetType (typeName);
if (type == null)
{
var types = from assembly in System.AppDomain.CurrentDomain.GetAssemblies ()
from assemblyType in assembly.GetTypes ()
where assemblyType.FullName == typeName
select assemblyType;
type = types.FirstOrDefault ();
}
return type;
}
If you just give a class name (which does need to be fully-qualified in terms of the namespace, of course)
Type.GetType(string)will only look in the currently executing assembly and mscorlib. If you want to get types from any other assembly, you need to specify the absolutely full name including the assembly information. As François says,Type.AssemblyQualifiedNameis a good way of seeing this. Here’s an example:Output:
Note that if you’re using a strongly named assembly (like
Formin this case) you must include all the assembly information – versioning, public key token etc.If you’re using a non-strongly-named assembly, it’s easier – something like:
for a type called
Bazin namespaceFoo.Bar, in assemblyMyCompany.MyAssembly. Note the absence of “.dll” at the end – that’s part of the filename, but not the assembly name.You should also be aware of the differences between C# names and CLR names for things like nested classes and generics. For example,
typeof(List<>.Enumerator)has a name ofSystem.Collections.Generic.List`1+Enumerator[T]. The generics side is tricky to work out, but the nested type bit is easy – it’s just represented with a “+” instead of the “.” you’d use in C#.