Type.GetType(string typeName) returns null !?
来源:互联网 发布:问卷调查数据分析报告 编辑:程序博客网 时间:2024/06/07 17:06
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.
- Type.GetType(string typeName) returns null !?
- Type.GetType() 返回null的解决办法
- Type.GetType()返回NULL的情况
- C# 关于 Type.GetType()
- typeof与Type.GetType()
- CFReadStreamCopyProperty() returns NULL?
- geoCoder.getFromLocationName returns null
- getActionBar() returns null
- RCP: getActiveWorkbenchWindow() returns null
- getResourceAsStream returns null
- getContentResolver().insert returns null
- Pb(100) The method getType() is undefined for the type new TypeToken<List<String>>(){}
- c# System.Type.GetType()方法
- 关于Type.GetType()的问题
- type() 和 GetType() 的区别
- DocumentBuilder.parse() returns [#Document: null]
- Oracle returns NULL after join
- BrowserManager Returns null - Flex 4.5
- 设计模式之观察者模式 【转】
- google protocol buffers介绍(c++)
- 转WaveOutSetVolume
- 她拒绝了他100次,但第101次他拒绝了她!
- magento获取指定目录分类的子分类
- Type.GetType(string typeName) returns null !?
- 监听用户电话状态
- 介绍一个Android开源项目:GifView——Android显示GIF动画
- Java 5.0 Thread
- subversion安装及使用
- 德安全专家的一个网址--此人是GSM破解高手
- c语言中通过函数指针实现函数重载
- 桌面宠物大全
- MySQL Replace 的使用