从 CLR 样式类型全名获取 C# 样式类型引用
给定通过反射找到的 .NET 类型对象,是否可以将该类型漂亮地打印或反编译为 C# 声明,同时考虑到 C# 类型别名等?
例如,
Int32 -> int
String -> string
Nullable<Int32> -> int?
List<au.net.ExampleObject> -> List<ExampleObject>
我希望能够打印出与源代码中最初编写的内容接近的方法。
如果.NET框架中没有任何东西,是否有第三方库?我可能会看看 ILSpy。
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.?
For example,
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.
If there isn't anything in the .NET framework, is there a third-party library? I might possibly have a look at ILSpy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅这个答案。
示例:
它将帮助您处理
int
和string
之类的东西,以及泛型,但是您必须解决可空-> T?
并使用
你自己。See this answer.
Example:
It will help you with
int
andstring
-like things, as well as generics, however you'll have to work outNullable<T> -> T?
andusing
s yourself.别名被编译为它们的别名。您永远不会知道源代码中它是
string
还是String
,坦率地说,我不明白为什么它很重要。Aliases are compiled to what they are an alias for. You will never know if it was
string
orString
in the source and frankly I can't see why it would matter.只有 15 个别名(+可为空)。只需对这些使用 string.Replace 即可。
There are only 15 aliases (+Nullable). Just use string.Replace on these.
中有类型声明的解决方案另一篇文章。
您可以轻松扩展它以支持类型别名。
There is a solution for type declarations in another post .
You can extend it to support type aliases easily.