从CLR样式类型全名获取C#样式类型引用 [英] Get C#-style type reference from CLR-style type full name

查看:121
本文介绍了从CLR样式类型全名获取C#样式类型引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到通过反射找到的.NET类型对象,是否有可能考虑到C#类型别名等因素将该类型漂亮地打印或反编译为C#声明?

Given a .NET type object found through reflection, is it possible to pretty print or decompile this type as a C# declaration, taking into account C# type aliases, etc.?

例如,

Int32 -> int
String -> string   
Nullable<Int32> -> int?
List<au.net.ExampleObject> -> List<ExampleObject>

我希望能够打印出接近原始代码的方法。

I want to be able to print out methods close to what was originally written in the source.

如果.NET框架中没有任何内容,是否有第三方库?我可能会看看 ILSpy

If there isn't anything in the .NET framework, is there a third-party library? I might possibly have a look at ILSpy.

推荐答案

请参见答案。

示例:

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>"

使用 int 和<类似code> string 的东西以及泛型,但是您必须计算 Nullable< T> -> T?自己使用

It will help you with int and string-like things, as well as generics, however you'll have to work out Nullable<T> -> T? and usings yourself.

这篇关于从CLR样式类型全名获取C#样式类型引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆