16.2. Retrieving the type from a name of a class
If you want to retrieve the Type of a class represented by a string instead, you can use the static GetType() method of the Type class. Examine the following code.
1: using System; 2: using System.Reflection; 3: 4: public class MyClass { 5: } 6: 7: public class MainClass { 8: public static void Main () { 9: Type t = Type.GetType("MyClass"); 10: Console.WriteLine(t); 11: } 12: }
You can try this to retrieve the Type representation of an int:
Type t = Type.GetType("System.Int32");
It is illegal to do the following though:
Type t = Type.GetType("int");
because int is just a C# alias for System.Int32. Type.GetType only takes in strings representing types of the CTS.
Alternatively, you can use ...
Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.