Type.GetType(string)

I have working on some reflection and CodeDom stuff in .net 2.0 . My requirement was to create  types at runtime by emitting in memory assemblies (i ll put the details later on) . At one place i encountered a naive and innocent looking function  Type.GetType(string) .  As i typed the method , intellisense popped in and suggested the usage of the method ” Get the Type with the specified name, performing a case-sensitive search.” (its in msdn), so i called  the method with  Type.GetType(“Font”) and to my amazement it returned. i was puzzled as  i was running a winform application and Font type is one of  important types  in winform apps.

I looked for the remarks section in msdn , it says  GetType method takes  a Type name and search for it in the current assembly or mscorlib, if you supply  Assembly Fully Qualified Name (AFQN), then it will load the assembly and search in it. i tried again by giving Typename, assembly name format , but i guess i made a mistake there, which led me to these pages

Type.GetType(string typeName) returns null !?  by  Haibo Lou
TypeName Grammar    by   Yiru

In summary , Type.AssemblyQualifiedName is the typename one should pass when calling Type.GetType(string) with public key token and culture. I think MS should update their documentation, so that API summary should state what it is expecting .
regards

Advertisements

2 responses to “Type.GetType(string)

  1. i have a problem i just have the Class Name as string … not the full AFQN (just like “Class1”).. I need to return the Type of that class…plz help me

  2. Thanks for the article. I too was getting back null so after I obtained my type in string format, I prepended the assembly qualified name to my type name like so:

    string stringType = “Customer”;

    string assemblyQualifiedName = “MySampleApplication.Entities.” + stringType;

    Type type = Type.GetType(assemblyQualifiedName, false, true);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s