Is it possible to obtain class summary at runtime?
Asked Answered
C

6

18

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.

Circumgyration answered 26/2, 2009 at 19:54 Comment(0)
P
6

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

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

Paryavi answered 26/2, 2009 at 20:0 Comment(2)
To elaborate your answer a bit: this solution actually makes a workaround virtually linking to the generated xml doc. Thus it works for your own code as well as most of the 3rd party libs (OSS and commercial). But there are some cases where you cannot obtain the xml doc and it is thus not generally possible.Particiaparticipant
Please include the relative details of your link; it'd be unfortunate if the link died in the future, rendering this answer useless - especially since it's the accepted answer.Retroaction
A
5

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

Antiquate answered 7/7, 2009 at 4:48 Comment(0)
N
3

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.

Nodarse answered 26/2, 2009 at 19:58 Comment(0)
E
3

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...
}
Ewald answered 26/2, 2009 at 19:59 Comment(1)
It is a solution, but if you use that, you must write twice your help. One in the attribute and other in the summary, if you want to read it on intellicense. :(Pierette
R
1

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.

Rotgut answered 26/2, 2009 at 19:57 Comment(1)
+1 for this solution. I did a clase that reads the xml. It also is good that if you miss a comment on a public method, it is warned so you don't miss it.Pierette
D
0

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.

Durative answered 26/2, 2009 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.