Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get C#-style type reference from CLR-style type full name

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.

like image 911
Nick Sonneveld Avatar asked Jun 07 '11 08:06

Nick Sonneveld


People also ask

What is the purpose of get C?

C library function - getc() The C library function int getc(FILE *stream) gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.

How does getc work in C?

The getc function in C reads the next character from any input stream and returns an integer value. It is a standard function in C and can be used by including the <stdio. h> header file.

Why does we use getc?

getc( ) function is used to read a character from file.

Where is get in C?

The gets function is part of the <stdio. h> header file in C. Function gets allows space-separated strings to be entered by the user. It waits until the newline character \n or an end-of-file EOF is reached.


1 Answers

See this answer.

Example:

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

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.

like image 158
Dan Abramov Avatar answered Sep 21 '22 03:09

Dan Abramov