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.
There are only 15 aliases (+Nullable). Just use string.Replace on these.
This article has c# source for a method which will do that: http://www.codeproject.com/Tips/621812/User-friendly-names-for-Types
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.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.There is a solution for type declarations in another post . You can extend it to support type aliases easily.