Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从CLR样式类型全名获取C#样式类型引用_C#_Reflection_Types_Decompiling_Pretty Print - Fatal编程技术网

从CLR样式类型全名获取C#样式类型引用

从CLR样式类型全名获取C#样式类型引用,c#,reflection,types,decompiling,pretty-print,C#,Reflection,Types,Decompiling,Pretty Print,给定一个通过反射找到的.NET类型对象,考虑到C类型别名等因素,是否可以将该类型作为C#声明进行漂亮的打印或反编译 比如说, Int32 -> int String -> string Nullable<Int32> -> int? List<au.net.ExampleObject> -> List<ExampleObject> Int32->int 字符串->字符串 可空->整数? 列表。中有一个用于类型声明的解决方案。 您

给定一个通过反射找到的.NET类型对象,考虑到C类型别名等因素,是否可以将该类型作为C#声明进行漂亮的打印或反编译

比如说,

Int32 -> int
String -> string   
Nullable<Int32> -> int?
List<au.net.ExampleObject> -> List<ExampleObject>
Int32->int
字符串->字符串
可空->整数?

列表。

中有一个用于类型声明的解决方案。
您可以轻松地扩展它以支持类型别名。

别名被编译为它们的别名。你永远不会知道它是源代码中的
string
还是
string
,坦率地说,我看不出它有什么关系。

参见答案

示例:

using System.CodeDom;
using System.CodeDom.Compiler;

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

var typeRef = new CodeTypeReference("System.Nullable`1[System.Int32]");
string typeOutput = provider.GetTypeOutput(typeRef); // "System.Nullable<int>"
使用System.CodeDom;
使用System.CodeDom.Compiler;
CodeDomProvider provider=CodeDomProvider.CreateProvider(“CSharp”);
var typeRef=new-CodeTypeReference(“System.Nullable`1[System.Int32]”);
字符串typeOutput=provider.GetTypeOutput(typeRef);//“System.Nullable”

它将帮助您处理
int
string
-类似的事情,以及泛型,但是您必须自己使用
Nullable->T?
。只需使用string.Replace即可。

我意识到这一点,我试图建议如果可能的话我更喜欢别名。是的,这就是为什么我考虑寻找c#反编译器的源代码。实际上,Jon Skeet在另一个问题中提到了CSharpCodeProvider。