आप CodeDom इस्तेमाल कर सकते हैं एक CodeVariableDeclarationStatement बनाने के लिए, और प्रकार संपत्ति को पुनः प्राप्त:
var stmt = new CodeVariableDeclarationStatement("string", "test");
string systemTypeName = stmt.Type.BaseType;
मुझे यकीन है कि है कि क्या आप CodeDom वर्गों है कि स्वतंत्र रूप से उपयोग कर सकते हैं नहीं हूँ, लेकिन यह स्विच का उपयोग कर एक देखने समारोह को बनाए बिना "System.String" को "स्ट्रिंग" से प्राप्त करने के लिए एक आसान तरीका होना चाहिए।
संपादित करें:
var systemTypeName = new CodeTypeReference("string").BaseType;
CodeVariableDeclarationStatement.Type संपत्ति एक CodeTypeReference है:
इसके बारे में अधिक सोच रही थी, यह संभव बस सीधे एक CodeTypeReference उपयोग करने के लिए, और इसके बाद के संस्करण की प्रक्रिया शॉर्टकट हो सकता है।सीधे CodeTypeReference का उपयोग करके, आपको डमी चर नाम से परेशान करने की आवश्यकता नहीं है, और यह एक-लाइनर बन सकता है।
काम उदाहरण:
मैं कोड है कि इस पहले किया था पाया। यह थोड़ा और अधिक जटिल की तुलना में मैं आशा व्यक्त की थी, लेकिन यह काम किया जाता है, और अगर उत्पन्न वर्गों कैश नहीं किया जाता, प्रदर्शन प्रारंभिक संकलन के बाद अच्छा है:
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<string, IDynamicTypeNameMapper> s_mappers;
static void Main(string[] args)
{
s_mappers = new Dictionary<string, IDynamicTypeNameMapper>();
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 < 1000; i++)
{
foreach (string keyword in csharpKeywords)
{
s_mappers[keyword].GetTypeName();
}
}
stopwatch.Stop();
Console.WriteLine("Cached time: " + stopwatch.Elapsed.ToString());
Console.ReadLine();
}
}
}
इस प्रकार इस एप्लिकेशन का उत्पादन किया जाता है के रूप में:
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
क्या आप इसे नलिकाओं के लिए भी काम करना चाहते हैं? उदाहरण के लिए। int? डबल,? इत्यादि – Gabriel