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.AssemblyQualifiedName
is a good way of seeing this. Here's an example:
using System;
using System.Windows.Forms;
class Test
{
static void Main()
{
string name = typeof(Form).AssemblyQualifiedName;
Console.WriteLine(name);
Type type = Type.GetType(name);
Console.WriteLine(type);
}
}
Output:
System.Windows.Forms.Form, System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
System.Windows.Forms.Form
Note that if you're using a strongly named assembly (like Form
in 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:
Foo.Bar.Baz, MyCompany.MyAssembly
for a type called Baz
in namespace Foo.Bar
, in assembly MyCompany.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 of System.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#.
As far as I know GetType looks for "Xyz" in an assembly named Foo.Bar.dll and I'm assuming it doesn't exist.
GetType relies on your passing the exact path to Xyz in the assembly.
Assembly and namespace don't have to be related.
Try System.Type type = System.Type.GetType(typeof(Foo.Bar.Xyz).AssemblyQualifiedName)
and see if that works.
The reason you find it with your LINQ example is that you are using GetAssemblies
which obtains the assemblies that have been loaded into the current execution context and thus has the details it needs to find all the types within the assemblies.
From the MSDN documentation (my emphasis):
If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. If the assembly has a strong name, a complete assembly name is required.
I just stumbled upon a similar problem and want to leave this here
First of all your can specify the AssemblyName in the string
var type = System.Type.GetType("Foo.Bar.Xyz, Assembly.Name");
However this only works for assemblies without a strong name. The explaination is already in Simons answer If the assembly has a strong name, a complete assembly name is required.
My problem was I had to resolve a System.Dictionary<?,?>
from a string at runtime. For a Dictionary<int, string>
this might be easy but what about a Dictionary<int, Image>
?
this would result in
var typeName = "System.Collections.Generic.Dictionary`2[System.Int32, [System.Drawing.Image, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]";
But I don't want to write the strong name. Especially because I don't want to include the versions since I am planning to target multiple frameworks with my code.
So here is my solution
privat statice void Main()
{
var typeName = "System.Collections.Generic.Dictionary`2[System.Int32, [System.Drawing.Image, System.Drawing]]";
var type = Type.GetType(typeName, ResolveAssembly, ResolveType);
}
private static Assembly ResolveAssembly(AssemblyName assemblyName)
{
if (assemblyName.Name.Equals(assemblyName.FullName))
return Assembly.LoadWithPartialName(assemblyName.Name);
return Assembly.Load(assemblyName);
}
private static Type ResolveType(Assembly assembly, string typeName, bool ignoreCase)
{
return assembly != null
? assembly.GetType(typeName, false, ignoreCase)
: Type.GetType(typeName, false, ignoreCase);
}
Type.GetType(...)
has an overload which acceps a func for assembly and type resolving which in neat. Assembly.LoadWithPartialName
is deprecated but if it's dropped in the future I could think of an replacement (iterate all assemblies in the current AppDomain and compare the partial names).