简体   繁体   English

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

[英]Is it possible to obtain class summary at runtime?

Is it possible to obtain class summary at runtime in C#? 是否可以在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. 在此先感谢您的帮助。

I once messed with this a while back, and used this guys solution. 我曾经一度搞砸了这个,并使用了这个家伙解决方案。 Worked pretty good: 工作得很好:

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

I maintain the Jolt.NET project on CodePlex and have implemented a feature that performs this very task. 我在CodePlex上维护了Jolt.NET项目,并实现了执行此任务的功能。 Please refer to the Jolt library for more information. 有关更多信息,请参阅Jolt库。

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 (ie MethodInfo , PropertyInfo , etc...). 从本质上讲,库允许您使用System.Reflection的元数据类型(即MethodInfoPropertyInfo等)以编程方式查找和查询程序集的XML doc注释文件。

Nope, they're not available through reflection. 不,他们通过反思无法获得。 See msdn : msdn

The XML doc comments are not metadata; XML文档注释不是元数据; they are not included in the compiled assembly and therefore they are not accessible through reflection. 它们不包含在已编译的程序集中,因此无法通过反射访问它们。

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. 有关运行时访问,请参阅创建属性创建的自定义属性(C#编程指南)使用反射访问属性(C#编程指南)

Example from MSDN: 来自MSDN的示例:

Author.cs: 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: SampleClass.cs:

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

You can, if you emit an XML documentation file. 如果发出XML文档文件,则可以。 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. 该过程将涉及使用反射来获取该类型的所有公共成员,然后使用XPath,从生成的XML文档中读取文档。

UPDATE: to include the XML doc in your dll/exe, just add it as an embedded resource, and compile twice if documentation changes. 更新:在您的dll / exe中包含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. Visual Studio可以在包含这些注释的输出文件夹(\\ bin \\ your_project.xml)中创建.xml文件。 If your application were distributed with that xml file then you would be able to access it programatically. 如果您的应用程序与该xml文件一起分发,那么您将能够以编程方式访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM