Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx" rel="noreferrer">list of built-in types in C#</a> is quite short and not very likely to change, so I think having a dictionary or a big <code>switch</code> statement to map these shouldn't be difficult to maintain.</p> <p>If you want to support nullable types, I believe you have no other option than to parse the input string:</p> <pre><code>static Type GetTypeFromNullableAlias(string name) { if (name.EndsWith("?")) return typeof(Nullable&lt;&gt;).MakeGenericType( GetTypeFromAlias(name.Substring(0, name.Length - 1))); else return GetTypeFromAlias(name); } static Type GetTypeFromAlias(string name) { switch (name) { case "bool": return typeof(System.Boolean); case "byte": return typeof(System.Byte); case "sbyte": return typeof(System.SByte); case "char": return typeof(System.Char); case "decimal": return typeof(System.Decimal); case "double": return typeof(System.Double); case "float": return typeof(System.Single); case "int": return typeof(System.Int32); case "uint": return typeof(System.UInt32); case "long": return typeof(System.Int64); case "ulong": return typeof(System.UInt64); case "object": return typeof(System.Object); case "short": return typeof(System.Int16); case "ushort": return typeof(System.UInt16); case "string": return typeof(System.String); default: throw new ArgumentException(); } } </code></pre> <p>Test:</p> <pre><code>GetTypeFromNullableAlias("int?").Equals(typeof(int?)); // true </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload