Type.GetType and referred assemblies

June 1, 2007

Problem Statement:

There is a DLL called CommonAssembly. This contains a namespace called CommonNamespace. This namespace contains a class called CommonClass.

Another project adds a reference to CommonAssembly and wants to get the type of CommonClass using the method Type.GetType.

Additional Information:

// This statement won’t work.
Type.GetType(“CommonClass”);

// This will not work either
Type.GetType(“CommonNamespace.CommonClass”);

Since CommonClass type is defined in a referred assembly, Type.GetType will not be able to get the required type.

Solution:

Type.GetType(“CommonNamespace.CommonClass, CommonAssembly”);

Anything that is given after the comma (“,”) is taken as the assembly name. This assembly is searched for the given type name.

If the required class is not encapsulated in any namespace, then omit the namespace name.

Type.GetType(“CommonClass, CommonAssembly”);

If this doesn’t work, then try giving the fully qualified name of the assembly.

9 Responses to “Type.GetType and referred assemblies”

  1. ManuelFelicio Says:

    You just saved my day!

  2. ManuelFelicio Says:

    It doesnt work..

  3. anaamica Says:

    Make sure you have given the correct qualified name. It should work.

  4. EZElmo Says:

    Type.GetType(”CommonClass”, CommonAssembly);

    should be

    Type.GetType(”CommonClass, CommonAssembly”);

    It’s right in the explaination….
    “Anything that is given after the comma (”,”) is taken as the assembly name.”
    Just not the example….

  5. anaamica Says:

    Oops, thanks for pointing it out, EzElmo.

  6. Alan Says:

    You can use:

    myObject.GetType().AssemblyQualifiedName

    to get the right string name to put in to Type.GetType()

  7. Garcia Says:

    Parabéns pelo Post!
    Salvou minha noite…
    Obrigado!

  8. Hamed Says:

    Thanks.
    I Saved my Job.
    I Love You.

  9. VinhPhat Says:

    Thanks you very much!

    you saved my life… I love U


Leave a Reply