C # - Is it possible to parse a type from a string - for example (pseudo-code) Type t = Type.Parse ("Int32");

Is it possible to parse a type from a string in C # - for example (pseudo-code)

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

This is an application that dynamically maps data from different formats to our internal format, and I need to be able to dynamically determine the type in order to do this.

(.NET 3.5)

+4
source share
2 answers

Yes, you want Type.GetType (a static method, not an instance inherited from object ).

For instance:

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

Please note that for types outside the current assembly or mscorlib you need to specify the type of the full name , which will be the full name (with namespace) and the display name of the assembly containing this type, separated by a comma, for example:

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

You need to use the full name, but you can use Type.GetType

 Type t = Type.GetType("System.Int32"); 
+1
source

Source: https://habr.com/ru/post/1337153/


All Articles