Type.GetType(string typeName) returns null !?
Type.GetType gives us the ability to get type back from a string. We pass a well-written type name string to it, and expect it return that type. Sometimes, to your surprise, it returns null. For example, Type.GetType("System.Data.SqlClient.SqlException").
If the assembly name is specified in the typeName string, Type.GetType() will search inside this assembly only; otherwise, it tries to find one in the caller assembly and then the system assembly (mscorlib.dll). Anything after ',' in the typeName string is treated as assembly name. In previous example, no assembly name was specified; neither the caller nor mscorlib had a type of SqlException, Type.GetType did indeed find nothing in its search. To make it work, we need specify System.Data.dll in typeName. In contrast, Type.GetType("System.Int32") can return type System.Int32 without mentioning mscorlib.dll.
Type.AssemblyQualifiedName is a right way to tell what kind of typeName we should provide. typeof(System.Data.SqlClient.SqlException).AssemblyQualifiedName is "System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this long string will succeed in .NET 2.0.
Note that in .NET, namespace and assembly name are not necessarily related. Type "System.Data.SqlClient.SqlException" is not with System.Data.SqlClient.dll (which does not exist, by the way). The separation is one reason why Type.GetType(typeName) tries not to guess which assembly should be loaded and then searched based on the type's namespace.
Other things good to mention here:
- To get back a generic type, the typeName needs be like "System.Collections.Generic.List`1", where the mark is `(grave accent), not '(quotation mark). Writing with the wrong mark will be likely to cause Type.GetType returning null too.
- If we prefer throwing exception to returning null, use the overload method Type.GetType(typeName, throwOnError)
- More information on this API, please go search MSDN. Yiru also has a further discussion on the type name grammar here.
Comments
Anonymous
February 20, 2007
Thanks, this saved me a bunch of time.Anonymous
March 21, 2007
PingBack from http://farazmahmood.wordpress.com/2007/03/21/typegettypestring/Anonymous
March 31, 2007
thank you so much for this article, i was going to get desparate ;)Anonymous
April 18, 2007
thanks for the info! solved my problem.Anonymous
April 19, 2007
great article..solved my problem.Anonymous
July 17, 2007
Thanks so much, this saved me a LOT of heartache.Anonymous
October 17, 2008
Recently I had to use Type.GetType to use Remote Objects in IIS and I kept running into a problem with