为给定 System.Type 的类定义生成源代码?

发布于 2025-01-02 04:51:31 字数 374 浏览 1 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦断已成空 2025-01-09 04:51:31

如果您只想生成像您所示的伪接口代码,您可以迭代公共字段和公共字段。像这样的属性:

string GetSourceCode(Type t)
{
    var sb = new StringBuilder();
    sb.AppendFormat("public class {0}\n{{\n", t.Name);

    foreach (var field in t.GetFields())
    {
        sb.AppendFormat("    public {0} {1};\n",
            field.FieldType.Name,
            field.Name);
    }

    foreach (var prop in t.GetProperties())
    {
        sb.AppendFormat("    public {0} {1} {{{2}{3}}}\n",
            prop.PropertyType.Name,
            prop.Name,
            prop.CanRead ? " get;" : "",
            prop.CanWrite ? " set; " : " ");
    }

    sb.AppendLine("}");
    return sb.ToString();
} 

对于类型:

public class MyType
{
    public int test;
    public string Name { get; set; }
    public int Age { get; set; }
    public int ReadOnly { get { return 1; } }
    public int SetOnly { set {} }
}

输出是:

public class MyType
{
   public Int32 test;
   public String Name { get; set; }
   public Int32 Age { get; set; }
   public Int32 ReadOnly { get; }
   public Int32 SetOnly { set; }
}

If you just want to generate pseudo-interface code like you have shown, you can iterate over the public fields & properties like this:

string GetSourceCode(Type t)
{
    var sb = new StringBuilder();
    sb.AppendFormat("public class {0}\n{{\n", t.Name);

    foreach (var field in t.GetFields())
    {
        sb.AppendFormat("    public {0} {1};\n",
            field.FieldType.Name,
            field.Name);
    }

    foreach (var prop in t.GetProperties())
    {
        sb.AppendFormat("    public {0} {1} {{{2}{3}}}\n",
            prop.PropertyType.Name,
            prop.Name,
            prop.CanRead ? " get;" : "",
            prop.CanWrite ? " set; " : " ");
    }

    sb.AppendLine("}");
    return sb.ToString();
} 

For the type:

public class MyType
{
    public int test;
    public string Name { get; set; }
    public int Age { get; set; }
    public int ReadOnly { get { return 1; } }
    public int SetOnly { set {} }
}

The output is:

public class MyType
{
   public Int32 test;
   public String Name { get; set; }
   public Int32 Age { get; set; }
   public Int32 ReadOnly { get; }
   public Int32 SetOnly { set; }
}
旧时模样 2025-01-09 04:51:31

尝试 .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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文