C# 如何从字符串中获取类型?

C# 如何从字符串中获取类型?,c#,.net,reflection,types,casting,C#,.net,Reflection,Types,Casting,为什么var type=type.GetTypeSystem.Windows.Forms.TextBox;返回空值 我试图从字符串中获取文本框类型。您还应该包括完整的程序集名称: var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); 请注意有关以下内容的文件:

为什么var type=type.GetTypeSystem.Windows.Forms.TextBox;返回空值


我试图从字符串中获取文本框类型。

您还应该包括完整的程序集名称:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
请注意有关以下内容的文件:

要获取的类型的程序集限定名。。。如果类型位于当前执行的程序集中或Mscorlib.dll中,则提供由其命名空间限定的类型名就足够了


因此,只能使用类型名称解析mscorlib和Assembly.getExecutionGassembly,否则您也需要完整的程序集名称。

您还应该包含完整的程序集名称:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
请注意有关以下内容的文件:

要获取的类型的程序集限定名。。。如果类型位于当前执行的程序集中或Mscorlib.dll中,则提供由其命名空间限定的类型名就足够了


因此,只能使用类型名称解析mscorlib和Assembly.getExecutionGassembly,否则您也需要完整的程序集名称。

您还应该包括程序集名称和公共令牌等:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

还应包括程序集名称和公共标记等:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

你不能在字符串上使用正则表达式吗?@PatrickHofman my bad,我以为他想要类的名称作为字符串。你不能在字符串上使用正则表达式吗?@PatrickHofman my bad,我以为他想要类的名称作为字符串。