programmatically get summary comments at runtime

programmatically get summary comments at runtime  using -'c#,,reflection,documentation'

I'm looking for a way to programmatically get the summary portion of Xml-comments of a method in

I have looked at the previous related posts and they do not supply a way of doing so in a web environment.

I can not use any 3rd party apps and due to a web environment, Visual studio plugin's arnt much use either.

The closest thing i have found to a working solution was the JimBlackler project, but it only works on DLL's.

Naturally, something like 'supply .CS file, get XML documentation' would be optimal.

Current situation

I have a web-service and im trying to dynammically generate documentation for it.

Reading the Methods, and properties is easy, but getting the Summary for each method is throwing me off a bit.

/// <summary>
/// This Is what im trying to read
/// </summary>
public class Some_Method(){}


asked Oct 6, 2015 by amit_cmps
0 votes

5 Answers

0 votes

The XML summary isn't stored in the .NET assembly - it's optionally written out to an XML file as part of your build (assuming you're using Visual Studio).

Consequently there is no way to "pull out" the XML summaries of each method via reflection on a compiled .NET assembly (either .EXE or .DLL) - because the data simply isn't there for you to pull out. If you want the data, you'll have to instruct your build environment to output the XML files as part of your build process and parse those XML files at runtime to get at the summary information.

answered Oct 6, 2015 by loknath.ganji
0 votes

You could 'document' your method using the System.ComponentModel.DataAnnotations.DisplayAttribute attribute, e.g.

[Display(Name = "Foo", Description = "Blah")]
void Foo()

then use reflection to pull the description at runtime.

answered Oct 6, 2015 by patilkiran.101
0 votes

A Workaround - Using reflection on Program.DLL/EXE together with Program.XML file

If you take a look at the sibling .XML file generated by Visual Studio you will see that there is a fairly flat hierarchy of /members/member. All you have to do is get hold on each method from your DLL via MethodInfo object. Once you have this object you turn to the XML and use XPATH to get the member containing the XML documentation for this method.

Members are preceded by a letter. XML doc for methods are preceded by "M:" for class by "T:" etc.

Load your sibling XML

string docuPath = dllPath.Substring(0, dllPath.LastIndexOf(".")) + ".XML";

if (File.Exists(docuPath))
  _docuDoc = new XmlDocument();

Use this xpath to get the member representing the method XML docu

string path = "M:" + mi.DeclaringType.FullName + "." + mi.Name;

XmlNode xmlDocuOfMethod = _docuDoc.SelectSingleNode(
    "//member[starts-with(@name, '" + path + "')]");

Now scan childnodes for all the rows of "///" Sometimes the /// Summary contains extra blanks, if this bothers use this to remove

var cleanStr = Regex.Replace(row.InnerXml, @"\s+", " ");
answered Oct 6, 2015 by param.oncemore
0 votes

If you have access to the source code you're trying to get comments for, then you can use Roslyn compiler platform to do that. It basically gives you access to all the intermediary compiler metadata and you can do anything you want with it.

It's a bit more complicated than what other people are suggesting, but depending on what your needs are, might be an option.

It looks like this post has a code sample for something similar.

answered Oct 6, 2015 by rajeshujade
0 votes

I agree with what your trying to do as C# XML comments allow the code be self-documenting (because the comments show up in intellisense when programming).

My work around is to do what Netricity recommended AND use the XML Comments. I type my comments in a specific format using the member attribute System.ComponentModel.DataAnnotations.DisplayAttribute. I then have a macro (via my Logitech G15 keyboard) that copies the display attribute comments and re-formats them into the standard XML comment syntax and places them appropriately. This way I have intellisense from the XMl comments, and I use the member attributes at run time to dynamically generate documentation for some special interfaces that I use.

This is a sloppy work around, but its the best I can come up with.

answered Oct 6, 2015 by vickeykumar66