question

kfy0002-3725 avatar image
0 Votes"
kfy0002-3725 asked kfy0002-3725 answered

How can I get a method discription

We can get the description of a property, such as:

Attribute attribute = memberInfo.GetCustomAttribute(typeof(DescriptionAttribute));
if (attribute != null)
{
DescriptionAttribute descAttr = attribute as DescriptionAttribute;
string description = descAttr.Description;
...
}

But this code is not applicable to MethodInfo, How can I get description(or summary) from a method?

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It seems to work. How to reproduce the problem?


0 Votes 0 ·

void SetMethodDrawingInfo(MethodInfo info)
{
lines.Clear();
LineDrawInfo line = new LineDrawInfo();

  //1. Draw method's body
  //1.1 typeName
  string typeName = MyConvert.GetTypeName(info.ReturnType);
  line.Add(typeName + " ", DrawPart.Type);
            
  //1.2 methodName
  line.Add( info.Name + "(", DrawPart.Other);

  //1.3 draw params
  ParameterInfo[] ps = info.GetParameters();
  int len = ps.Length;
  string text = null;
  if (ps.Length > 0)
  {
      int index = 0;
      foreach (ParameterInfo p in ps)
      {
          ...
      }
  }

  line.Add(")", DrawPart.Other);

  if (overloads > 1)
  {
      text = $"(+{overloads}重载)";
      line.Add(text, DrawPart.Other);
  }

  //
  lines.Add(line);

  //2. draw description          
  //2.1 get the DescriptionAttribute

  string desc = null;
  Attribute attribute = info.GetCustomAttribute(typeof(DescriptionAttribute));
  if (attribute != null)
  {
      DescriptionAttribute descAttr = attribute as DescriptionAttribute;
      desc = descAttr.Description;
  }
  if (desc == null)
      return;              
  //2.2 
  line = new LineDrawInfo();
  line.Add(desc, DrawPart.Other);
  lines.Add(line);

}

0 Votes 0 ·

@kfy0002-3725
There is no field or property named Description in MethodInfo, we can't get it.
You get DescriptionAttribute.Description because you (the author) defined this property in this custom attribute.
Did I misunderstand what you mean?
Could you please show us the code of DescriptionAttribute?

0 Votes 0 ·
 void SetMethodDrawingInfo(MethodInfo info)
 {
     lines.Clear();
     LineDrawInfo line = new LineDrawInfo();

     //1. Draw method's body
     //1.1 typeName
     string typeName = MyConvert.GetTypeName(info.ReturnType);
     line.Add(typeName + " ", DrawPart.Type);
            
     //1.2 methodName
     line.Add( info.Name + "(", DrawPart.Other);

     //1.3 draw params
     ParameterInfo[] ps = info.GetParameters();
     int len = ps.Length;
     string text = null;
     if (ps.Length > 0)
     {
         int index = 0;
         foreach (ParameterInfo p in ps)
         {
             ...
         }
     }

     line.Add(")", DrawPart.Other);

     if (overloads > 1)
     {
         text = $"(+{overloads}重载)";
         line.Add(text, DrawPart.Other);
     }

     //
     lines.Add(line);

     //2. draw description          
     //2.1 get the DescriptionAttribute

     string desc = null;
     Attribute attribute = info.GetCustomAttribute(typeof(DescriptionAttribute));
     if (attribute != null)
     {
         DescriptionAttribute descAttr = attribute as DescriptionAttribute;
         desc = descAttr.Description;
     }
     if (desc == null)
         return;              
     //2.2 
     line = new LineDrawInfo();
     line.Add(desc, DrawPart.Other);
     lines.Add(line);
 }
0 Votes 0 ·

In my app:

115734-001.png![115752-002.png


Visual studio

![115713-0003.png][3]




How is Visual studio done?

0 Votes 0 ·
001.png (14.7 KiB)
002.png (12.8 KiB)
0003.png (16.2 KiB)
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

We can add a specific xml summary as a comment for the method (class, etc.), and then Visual Studio will get it correctly.

         /// <summary>
         /// This is a test method.
         /// </summary>
         public void PrintHW() 
         {
             Console.WriteLine("Hello World!");
         }

115708-11.png

Programmatically get Summary comments at runtime

c# getting interface method comments


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


11.png (5.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kfy0002-3725 avatar image
0 Votes"
kfy0002-3725 answered

TimonYang-MSFT:

I Will show the summary in my code editor.

I used "get method summary info" as the keyword to search the relevant web pages on the Internet and found some articles.


thank you!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.