为什么var type = Type.GetType("System.Windows.Forms.TextBox");
返回null
?如何从字符串中获取类型?
我想从string
获得TextBox
类型的类型。
为什么var type = Type.GetType("System.Windows.Forms.TextBox");
返回null
?如何从字符串中获取类型?
我想从string
获得TextBox
类型的类型。
你应该包括完整的程序集名称太:
var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
注上MSDN(重点煤矿)的文档:
的集限定名的类型来获得的。 ...如果类型位于当前正在执行程序集的或Mscorlib.dll中,则提供通过名称空间限定的类型名称即可。
所以只有mscorlib和Assembly.GetExecutingAssembly()
可以使用类型名称来解析,否则你也需要全部程序集名称。
你还应该包括组件名称和公共令牌等:
var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
你就不能使用的字符串正则表达式? – nafas 2014-10-06 11:15:59
@PatrickHofman我的不好,我以为他想把班级的名字当作“String”。 – nafas 2014-10-06 11:25:46