简体   繁体   English

C# - 是否可以从字符串中解析Type - 例如(伪代码)类型t = Type.Parse(“Int32”);

[英]C# - Is it possible to parse a Type from a string - e.g (Pseudo code) Type t = Type.Parse(“Int32”);

Is it possible to parse a Type from a string in C# - eg (Pseudo code) 是否可以从C#中的字符串解析Type - 例如(伪代码)

Type t = Type.Parse("Int32");

This is for an application that dynamically maps data from varying formats to our inhouse format, and I need to be able to dynamically determine type to do this. 这适用于将来自不同格式的数据动态映射到我们的内部格式的应用程序,我需要能够动态确定要执行此操作的类型。

(.NET 3.5) (.NET 3.5)

Yes, you want Type.GetType (the static method, not the instance one inherited from object ). 是的,您需要Type.GetType (静态方法,而不是从object继承的实例)。

For example: 例如:

Type t = Type.GetType("System.Int32");

Note that for types outside the current assembly or mscorlib, you'll need to specify the type's fully qualified name , which will be the full name (with namespace) and the display name of the assembly containing that type, separated by a comma—for example: 请注意,对于当前程序集或mscorlib之外的类型,您需要指定类型的完全限定名称 ,该名称将是全名(带名称空间) 包含该类型的程序集的显示名称,用逗号分隔例:

Type t = Type.GetType("System.Collections.Specialized.StringCollection,System");

您必须使用完全限定名称,但可以使用Type.GetType

Type t = Type.GetType("System.Int32");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM