Is there a way in .NET to create the source code class definition given a System.Type?
public class MyType
{
public string Name { get; set; }
public int Age { get; set; }
}
string myTypeSourceCode = GetSourceCode( typeof(MyType) );
Basically I’m looking for what GetSourceCode() is.
I realize there would be limitations: If there are property getters/setters or private members the source is not included, but I don’t need that. Assume the type is a Data Transfer Object, so just the public properties/fields need to be exposed.
What I’m using this for is auto-generated code examples for a web API.
If you just want to generate pseudo-interface code like you have shown, you can iterate over the public fields & properties like this:
For the type:
The output is: