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