是否可以在运行时获取类摘要?

发布于 2024-07-14 02:15:08 字数 259 浏览 9 评论 0原文

C# 是否可以在运行时获取类摘要? 我想通过反射获取类摘要,然后将其写入控制台。 我所说的类摘要是指类定义之前的摘要注释,如下所示:

/// <summary>
/// some description
/// </summary>
class SomeClass
{
}

我不知道这些注释在编译代码后是否可用,但如果可用,也许有一种方法可以在代码中获取它们。

预先感谢您的帮助。

Is it possible to obtain class summary at runtime in C#?
I would like to obtain class summary through reflection and then write it to console.
By class summary I mean summary comments before class definition, something like this:

/// <summary>
/// some description
/// </summary>
class SomeClass
{
}

I don't know if these comments are available after compiling the code, but if they are maybe there is a way to obtain them in code.

Thanks in advance for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

南街九尾狐 2024-07-21 02:15:08

不久前我曾经搞乱过这个问题,并使用了这个人的解决方案。 效果很好:

http://jimblackler.net/blog/?p=49

I once messed with this a while back, and used this guys solution. Worked pretty good:

http://jimblackler.net/blog/?p=49

荒芜了季节 2024-07-21 02:15:08

我在 CodePlex 上维护 Jolt.NET 项目,并实现了执行此任务的功能。 请参阅 Jolt 库了解更多信息。

实质上,该库允许您使用 System.Reflection 中的元数据类型(即 MethodInfoPropertyInfo)以编程方式查找和查询程序集的 XML 文档注释文件。 等...)。

I maintain the Jolt.NET project on CodePlex and have implemented a feature that performs this very task. Please refer to the Jolt library for more information.

In essence, the library allows you to programatically locate and query an XML doc comments file for an assembly using the metadata types in System.Reflection (i.e. MethodInfo, PropertyInfo, etc...).

时光清浅 2024-07-21 02:15:08

不,它们无法通过反射获得。 请参阅 msdn

XML 文档注释不是元数据;而是元数据。
它们不包含在编译中
组装,因此它们不是
通过反射访问。

Nope, they're not available through reflection. See msdn:

The XML doc comments are not metadata;
they are not included in the compiled
assembly and therefore they are not
accessible through reflection.

離殇 2024-07-21 02:15:08

您无法在运行时访问它们,因为编译器将它们视为注释。

但是,如果您想使用属性来指定信息并在运行时通过反射访问它,您可以这样做。

有关属性创建和 创建自定义属性(C# 编程指南),请参阅a href="http://msdn.microsoft.com/en-us/library/z919e8tw.aspx" rel="nofollow noreferrer">通过反射访问属性(C# 编程指南) 用于运行时访问。

来自 MSDN 的示例:

Author.cs:

public class Author : System.Attribute
{
    private string name;
    public double version;

    public Author(string name)
    {
        this.name = name;
        version = 1.0;
    }
}

SampleClass.cs:

[Author("H. Ackerman", version = 1.1)]
class SampleClass
{
    // H. Ackerman's code goes here...
}

You cannot access those at runtime because those are considered to be comments by the compiler.

However, if you wanted to use an Attribute to specify information and access it during runtime via reflection you could do that.

See Creating Custom Attributes (C# Programming Guide) for attribute creation and Accessing Attributes With Reflection (C# Programming Guide) for runtime access.

Example from MSDN:

Author.cs:

public class Author : System.Attribute
{
    private string name;
    public double version;

    public Author(string name)
    {
        this.name = name;
        version = 1.0;
    }
}

SampleClass.cs:

[Author("H. Ackerman", version = 1.1)]
class SampleClass
{
    // H. Ackerman's code goes here...
}
猫九 2024-07-21 02:15:08

如果您发出 XML 文档文件,则可以。 该过程将涉及使用反射来获取该类型的所有公共成员,然后使用 XPath 从生成的 XML 文档中读取文档。

更新:要将 XML 文档包含在 dll/exe 中,只需将其添加为嵌入式资源,并在文档发生更改时编译两次。

You can, if you emit an XML documentation file. The process would involve using reflection to get all the public members of the type, then using XPath, read the documentation from the generated XML document.

UPDATE: to include the XML doc in your dll/exe, just add it as an embedded resource, and compile twice if documentation changes.

暮凉 2024-07-21 02:15:08

不,这些注释不包含在您编译的程序集中。

Visual Studio 可以在输出文件夹 (\bin\your_project.xml) 中创建一个包含这些注释的 .xml 文件。 如果您的应用程序随该 xml 文件一起分发,那么您将能够以编程方式访问它。

No, those comments are not included in your compiled assembly.

Visual Studio can create a .xml file in your output folder (\bin\your_project.xml) which contains those comments. If your application were distributed with that xml file then you would be able to access it programatically.

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