views:

176

answers:

4

Hello, how to recognize a comment in the C# source code? I want to retrieve all information from the comment.

public class TestClass
    {
        /// <summary>
        /// Sample method
        /// </summary>
        /// <param name="a">1 argument</param>
        /// <param name="b">2 argument</param>
        /// <param name="c">3 argument</param>
        /// <returns>true or false</returns>
        /// <exception cref="NotImplementedException">Always throw exception</exception>
        public bool Method(int a, object b, Panel c)
        {
            throw new NotImplementedException();
        }
    }
  1. Description of the method - "Sample method"
  2. Description of the parameters - "1 argument, ..."
  3. Description of the returned value - "true or false"
  4. Exception type and description
  5. Other user tags
+5  A: 

The easiest way would be to enable xml comment generation (project properties -> build), and parse the xml file...

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>ClassLibrary2</name>
    </assembly>
    <members>
        <member name="M:TestClass.Method(System.Int32,System.Object,System.Windows.Forms.Panel)">
            <summary>
            Sample method
            </summary>
            <param name="a">1 argument</param>
            <param name="b">2 argument</param>
            <param name="c">3 argument</param>
            <returns>true or false</returns>
            <exception cref="T:System.NotImplementedException">Always throw exception</exception>
        </member>
    </members>
</doc>
Marc Gravell
But what can I do if I do not have the XML file?It can use regular expressions?
mykhaylo
Well, if you have the C#, you can always compile it to *get* the xml file... you can try regex, but it won' be easy.
Marc Gravell
But I do not have access to the project and I can not compile it. I have only certain files.
mykhaylo
A: 

I'm not sure how your planning on presenting this data, but there is an open source project called nDoc that formats this information nicely in a webpage.

In order to use nDoc you need to enable xml output in your the project settings. This will get your all your comments in an XML format.

Even if you not using nDoc they some nice instructions on enabling XML Output

bendewey
A: 

Marc's answer is your best bet. In case you wanted (for some reason) to do it manually, you need to look for continuous runs of lines beginning with ///, concatenate them and then treat that string as XML.

Daniel Earwicker
+1  A: 

If you really want to get gung ho, you can build a parser. I guess it depends on what you're trying to achieve, why, and what resources / time you have to put into it. If you're going to try to parse C#, consider ANTLR or another parser/compiler generator.

I'm jumping to the opposite end of the complexity spectrum, but I don't know your situation, so it may be appropriate.

Comments can be tricky without a full-blown parser. Consider these edge cases:

// Blah blah // another double slash on the same line.
/* 
 What about multi-line comments? 
*/

/* 
   // What about double-slash comments inside of multi-line comments?
*/

(And I'm just scratching the surface with these edge cases).

Charlie Flowers