为给定 System.Type 的类定义生成源代码?
.NET 中有没有办法在给定 System.Type 的情况下创建源代码类定义?
public class MyType
{
public string Name { get; set; }
public int Age { get; set; }
}
string myTypeSourceCode = GetSourceCode( typeof(MyType) );
基本上我正在寻找 GetSourceCode() 是什么。
我意识到会有限制:如果有属性获取器/设置器或私有成员,则不包含源代码,但我不需要它。假设该类型是数据传输对象,因此只需公开公共属性/字段。
我用它来自动生成 Web API 的代码示例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想生成像您所示的伪接口代码,您可以迭代公共字段和公共字段。像这样的属性:
对于类型:
输出是:
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:
尝试 .Net 反编译器
这里有一些 .net 反编译器的链接
http://www.telerik.com/products/decompiler.aspx
http://www.jetbrains.com/decompiler/
http://www.devextras.com/decompiler/
http://wiki.sharpdevelop.net/ilspy.ashx
或者也许您可以找到免费的旧版本 .Net Reflector...
Try a .Net decompiler
Here are some links to .net decompiler
http://www.telerik.com/products/decompiler.aspx
http://www.jetbrains.com/decompiler/
http://www.devextras.com/decompiler/
http://wiki.sharpdevelop.net/ilspy.ashx
or maybe you'll be able to find an old version of .Net Reflector when it was free...