Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the <a href="http://msdn.microsoft.com/en-us/library/system.codedom.aspx" rel="nofollow noreferrer">CodeDom</a> to create a <a href="http://msdn.microsoft.com/en-us/library/system.codedom.codevariabledeclarationstatement.aspx" rel="nofollow noreferrer">CodeVariableDeclarationStatement</a>, and retrieve the Type property:</p> <pre><code>var stmt = new CodeVariableDeclarationStatement("string", "test"); string systemTypeName = stmt.Type.BaseType; </code></pre> <p>I am not sure whether you can use CodeDom classes that independently, but it should be an easy way to get from "string" to "System.String" without needing to create a lookup function using <em>switch</em>.</p> <p>EDIT:</p> <p>Thinking about it more, it <em>may</em> be possible to simply use a <a href="http://msdn.microsoft.com/en-us/library/system.codedom.codetypereference.aspx" rel="nofollow noreferrer">CodeTypeReference</a> directly, and shortcut the process above:</p> <pre><code>var systemTypeName = new CodeTypeReference("string").BaseType; </code></pre> <p>The CodeVariableDeclarationStatement.Type property is a CodeTypeReference. By using CodeTypeReference directly, you don't need to bother with a dummy variable name, and it can become a one-liner.</p> <p>WORKING EXAMPLE:</p> <p>I found the code that did this previously. It is a bit more complicated than I had hoped, but it gets the job done, and if the generated classes are cached, performance is good after initial compilation:</p> <pre><code>using System; using System.CodeDom; using System.Collections.Generic; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Diagnostics; namespace ConsoleApplication1 { public interface IDynamicTypeNameMapper { string GetTypeName(); } class Program { static readonly string[] csharpKeywords = new[] { "byte", "short", "int", "long", "float", "double", "string" }; static Dictionary&lt;string, IDynamicTypeNameMapper&gt; s_mappers; static void Main(string[] args) { s_mappers = new Dictionary&lt;string, IDynamicTypeNameMapper&gt;(); var provider = new CSharpCodeProvider(); var options = new CompilerParameters(); options.ReferencedAssemblies.Add("ConsoleApplication1.exe"); options.GenerateInMemory = true; var stopwatch = new Stopwatch(); stopwatch.Start(); foreach (string keyword in csharpKeywords) { string className = "DynamicTypeNameMapper_" + keyword; string literal = "using System; using ConsoleApplication1; namespace Test { public class " + className + ": IDynamicTypeNameMapper { public string GetTypeName() { return typeof(" + keyword + ").FullName; } } }"; var snippet = new CodeSnippetCompileUnit(literal); var results = provider.CompileAssemblyFromDom(options, snippet); var typeNameMapper = results.CompiledAssembly.CreateInstance("Test." + className) as IDynamicTypeNameMapper; if (typeNameMapper != null) { s_mappers.Add(keyword, typeNameMapper); Console.WriteLine(typeNameMapper.GetTypeName()); } } stopwatch.Stop(); Console.WriteLine("Inital time: " + stopwatch.Elapsed.ToString()); stopwatch.Reset(); stopwatch.Start(); for (int i = 0; i &lt; 1000; i++) { foreach (string keyword in csharpKeywords) { s_mappers[keyword].GetTypeName(); } } stopwatch.Stop(); Console.WriteLine("Cached time: " + stopwatch.Elapsed.ToString()); Console.ReadLine(); } } } </code></pre> <p>The output of this app is as follows:</p> <pre><code>System.Byte System.Int16 System.Int32 System.Int64 System.Single System.Double System.String Inital time: 00:00:00.3090559 Cached time: 00:00:00.0011934 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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