如何重用Type返回值来生成新的源代码

pas*_*epe 5 c# .net-2.0

我有这个问题:

我想从对象信息生成一个新的源代码文件.

我做这样的事情:

dictParamMapping是Dictionary<string, Type>字符串是变量名称的地方,Type是该变量的类型.

我得到了使用以下方法构建新代码的类型:

dictParamMapping[pairProcess.Value].Type.Name (or FullName)
Run Code Online (Sandbox Code Playgroud)

当Type是int,string,datatable等......一切正常但是当它是一个字典,列表或任何其他类似的东西时它会返回如下内容:

System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Run Code Online (Sandbox Code Playgroud)

构建新的源代码是不正确的.我想得到类似的东西Dictionary<int, double>

谁能帮我?

Ani*_*Ani 3

您似乎想要从System.Type. 你可以在CSharpCodeProvider课堂上这样做。

var typeReference = new CodeTypeReference(typeof(Dictionary<int, double>));

using (var provider = new CSharpCodeProvider())
    Console.WriteLine(provider.GetTypeOutput(typeReference));
Run Code Online (Sandbox Code Playgroud)

输出:

System.Collections.Generic.Dictionary<int, double>
Run Code Online (Sandbox Code Playgroud)

请注意,这并不能保证始终为您提供有效的类型名称。特别是,编译器生成的类型(例如闭包)通常具有在 C# 中无效的类型名称。