Is it possible to obtain class summary at runtime?

孤人 提交于 2019-11-28 09:47:43
BFree

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

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

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...).

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.

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...
}

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!