c# getting interface method comments

2020-02-03 06:24发布

问题:

Say I have

    interface IFoo
    {
        /// <summary>
        /// Comments about Bar method goes here.
        /// </summary>
        void Bar();
    }

I'm using reflection to display the methods at runtime

MethodInfo[] mis = typeof(IFoo).GetMethods();

but I was wondering if I can get the comments included in <summary> </summary> for the methods. I realize that comments are just ignored my the compiler but is there anything that could be done to retrieve comments? Right now I have a seperate file that has the methods and the comments but I hate the redundancy and was wondering if there is any way to do this.

Thanks,

回答1:

The C# compiler csc.exe has a /doc option that outputs an external XML file having your triple-slash comments. This XML file is used by documentation generators (e.g. Sandcastle does this kind of thing).

That same option to export XML comments is available from Visual Studio. To set this compiler option in the Visual Studio development environment:

  1. Open the project's Properties page. For details, see How to: Set Project Properties (C#, J#).
  2. Click the Build property page.
  3. Modify the XML Documentation File property.

You can load up this XML file using an XML parser from the .NET framework, access the Types in it, and grab the related comments from around them.

You're right the C# compiler doesn't compile comments into the meta data. However Microsoft created triple-slash comments for export ability, so you can get a handle to them.

Instructions for processing the XML file are here on MSDN.


As an example, I enable the XML output file option and documented the following method:

/// <summary>
/// This method parses the given name for
/// capitalization.
/// </summary>
public void ParseStringCase(string name)
{
    // behaviour of method...
}

It produces the following XML in a file in the bin/ folder....

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>WindowsFormsApplication3</name>
    </assembly>
    <members>
        <member name="M:WindowsFormsApplication3.Form1.ParseStringCase(System.String)">
            <summary>
            This method parses the given name for
            capitalization.
            </summary>
        </member>
    </members>
</doc>


回答2:

You could also make an attribute class and get the information that way. So your method/interface would look like this:

 [AttributesTest("Test", "Test comments")]
 public void Method(object sender, EventArgs e)
 {
     //do something here
 }

Your attribute class would then look like:

[AttributeUsage(AttributeTargets.Method)]
  sealed class AttributesTest : Attribute 
  {
    public string sName;
    public string sDescription;

    public string Name
    {
      get { return sName; }
      set { sName = value; }
    }

    public string Description
    {
      get { return sDescription; }
      set { sDescription = value; }
    }

    public AttributesTest(string _name, string _desc)
    {
      this.Name = _name;
      this.Description = _desc;
    }
  }

Then you can retrieve the Name and Description for each interface/method. You are not really getting the comments, but it will get whatever information you specify. I find this helpful when I need to grab information about a method/interface in my programs. I hope this helps!



回答3:

To add to John K's answer: It is not possible to retrieve the comments using reflection. You always need a separate file supplying the comments. Using the XML document file from the compiler is the best way, because it is the default format for this information.



回答4:

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

For details of how to implement John K. plan, look here



标签: c# reflection