Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

Is it possible to parse a Type from a string in C# - e.g (Pseudo code)

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)

like image 660
gb2d Avatar asked Dec 21 '22 18:12

gb2d


1 Answers

Yes, you want Type.GetType (the static method, not the instance one inherited from 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:

Type t = Type.GetType("System.Collections.Specialized.StringCollection,System");
like image 190
Dan Tao Avatar answered Dec 24 '22 08:12

Dan Tao