Brandon Brandon - 11 months ago 60
C# Question

Resolve Type from Class Name in a Different Assembly

I have a method where I need to resolve the Type of a class. This class exists in another assembly with the namespace similar to:


I am attempting to perform the following:

Type.GetType("MyProject.Domain.Model." + myClassName);

This works great if the code that is performing this action is in the same assembly as the class whose type I am trying to resolve, however, if my class is in a different assembly, this code fails.

I am sure there is a far better way to accomplish this task, but I have not had a lot of experience with resolving assemblies and traversing namespaces within to resolve the type of the class I am looking for. Any advice or tips to accomplish this task more gracefully?

Answer Source

You'll have to add the assembly name like this:

Type.GetType("MyProject.Domain.Model." + myClassName + ", AssemblyName");

To avoid ambiguity or if the assembly is located in the GAC, you should provide a fully qualified assembly name like such:

Type.GetType("System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089");