How can I get a method discription

kfy0002 1 Reputation point
2021-07-17T00:01:17.507+00:00

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?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-07-19T08:24:27.947+00:00

    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.

    0 comments No comments

  2. kfy0002 1 Reputation point
    2021-07-20T06:40:48.933+00:00

    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!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.