I'm trying to type of an unknown interface. typeof(A.B.C.Foo)
gives me its type but Type.GetType("A.B.C.Foo")
is null. How can I get the type of an unknown interface?

- 8,362
- 12
- 66
- 106
-
1Examine the value of `typeof(A.B.C.Foo).AssemblyQualifiedName`. That value is the string you need to pass to `Type.GetType`. – Michael Liu Nov 06 '13 at 14:44
4 Answers
You can find a type in assembly(s) and so get the instance of that Type instance:
C#: List All Classes in Assembly by using Assembly.GetTypes
So if you want to define some plugin-based architecture with dependency injection, you have to define in some manifest file the type you expose and, better, assembly it located in.
Type.GetType
only works without qualification if the class is in the currently executing assembly or located in mscorelib.dll. If this is not the case, use the fully qualified name of the assembly along with the type information to obtain the type. E.G.:
static void Main(string[] args)
{
var result1 = typeof (ClassLibrary2.Class1);
var result2 = Type.GetType("ClassLibrary2.Class1");//returns null because Class1 is not in the currently executing assembly or mscorlib
var assembly = Assembly.GetAssembly(typeof(ClassLibrary2.Class1));
var result3 = Type.GetType("ClassLibrary2.Class1, " + assembly.FullName);
Console.WriteLine("{0}, {1}, {2}", result1, result2, result3);
}
Here Main is in a console application. ClassLibrary2.Class1
is located in a separate class library.

- 43,228
- 68
- 238
- 348
Type.GetType(string)
expects the assembly-qualified name of the type as a parameter, and you're just passing the type's full name. The only time when you don't need to pass the assembly-qualified name is if the type is in the currently executing assembly or in mscorlib.dll.
If you don't know which assembly it's located in, you can iterate over all of the loaded assemblies with AppDomain.CurrentDomain.GetAssemblies()
and use Assembly.GetType()
for each assembly until you've found the requested type.

- 24,731
- 12
- 95
- 110
GetType(string) requires you to specify the AssemblyQualifiedName (usually the top level namespace, any sub level namespace, and the class name) if it's from a referenced assembly. If it's within the same assembly, only the namespace and the class name is required.
MSDN example of a AssemblyQualifiedName:
TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly
Simple example of a project referencing the project LibraryTest
with the class Foo
:
var type = Type.GetType("LibraryTest.Foo, LibraryTest");

- 6,112
- 4
- 37
- 43