Alex Rivera | Logout

Getting interface method comments in C#

Asked 2011-05-02T15:34:44.703
19

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 something that could be done to retrieve comments?

Right now, I have a separate file that has the methods and the comments, but I hate the redundancy and was wondering if there is a way to do this.

Edit
Report

1 Answer

7

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.

answered 2011-05-02T15:40:13.940

Your Answer