views:

33

answers:

3

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:

MyProject.Domain.Model

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?

+3  A: 

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=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
Sandor Drieënhuizen
Excellent, I knew I was missing something minor like including the assembly. This solution worked for my needs. Thanks.
Brandon
+1  A: 

Can you use either of the standard ways?

typeof( MyClass );

MyClass c = new MyClass();
c.GetType();

If not, you will have to add information to the Type.GetType about the assembly.

Jerod Houghtelling
A: 

Your code should work if your project references the desired assemblies.

codymanix