how to get the anotate of class or property?

mc 4,111 Reputation points
2024-03-25T01:45:42.91+00:00
public MyClassA

{

/// <summary>

/// this is my summary I want to get

/// </summary>

public int Id{get;set;}

}

how to get the summary? using assembly.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
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,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-03-25T07:19:18.74+00:00

    Hi @mc , Welcome to Microsoft Q&A,

    In Visual Studio, you need to make sure Generate XML documentation comments is enabled and that the XML file is in the correct output directory.

    Please follow these steps to check and set XML document comments:

    Enable generating XML documentation comments in the project properties. In Visual Studio, right-click the project and select Properties. Go to the "Build" tab. Make sure "XML Document File" is checked.

    User's image

    enter image description here

    using System;
    
    namespace xxx
    {
    
        internal class Program
        {
    
            public static void Main(string[] args)
            {
                Type type = typeof(MyClassA);
                var classSummary = GetClassSummary(type);
                var idSummary = GetMemberSummary(type, "Id");
    
                Console.WriteLine("Class summary:");
                Console.WriteLine(classSummary);
    
                Console.WriteLine("\nId property summary:");
                Console.WriteLine(idSummary);
    
                Console.ReadLine();
            }
    
            public static string GetClassSummary(Type type)
            {
                var assembly = type.Assembly;
                var xmlFilePath = $"{assembly.GetName().Name}.xml";
                var xmlDocumentation = new System.Xml.XmlDocument();
                xmlDocumentation.Load(xmlFilePath);
    
                var classNode = xmlDocumentation.SelectSingleNode($"/doc/members/member[starts-with(@name, 'T:{type.FullName}')]");
                if (classNode != null)
                {
                    var summaryNode = classNode.SelectSingleNode("summary");
                    if (summaryNode != null)
                    {
                        return summaryNode.InnerText.Trim();
                    }
                }
    
                return "Summary not found.";
            }
    
            public static string GetMemberSummary(Type type, string memberName)
            {
                var assembly = type.Assembly;
                var xmlFilePath = $"{assembly.GetName().Name}.xml";
                var xmlDocumentation = new System.Xml.XmlDocument();
                xmlDocumentation.Load(xmlFilePath);
    
                var memberNode = xmlDocumentation.SelectSingleNode($"/doc/members/member[starts-with(@name, 'P:{type.FullName}.{memberName}')]");
                if (memberNode != null)
                {
                    var summaryNode = memberNode.SelectSingleNode("summary");
                    if (summaryNode != null)
                    {
                        return summaryNode.InnerText.Trim();
                    }
                }
    
                return "Summary not found.";
            }
        }
        /// <summary>
        /// This is my MyClassA 
        /// </summary>
        public class MyClassA
        {
            /// <summary>
            ///  Id property
            /// </summary>
            public int Id { get; set; }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 6,300 Reputation points
    2024-03-25T02:51:35.02+00:00

    The summary part is not included in the assembly, so you can only read the xml. If it is an attribute, it can be obtained from the assembly.

    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Xml.Linq;
    
    class Program
    {
        /// <summary>
        /// this is my summary I want to get
        /// </summary>
        /// <remarks>
        /// this is remarks.
        /// </remarks>
        [Description("this is my description")]
        public int Id { get; set; }
    
        static void Main() {
            // Get PropertyInfo
            PropertyInfo propertyInfo = typeof(Program).GetProperty("Id");
            // Anotation
            Console.WriteLine(GetPropertyAnotation(propertyInfo, "summary"));
            Console.WriteLine(GetPropertyAnotation(propertyInfo, "remarks"));
            // Attribute
            Console.WriteLine(propertyInfo.GetCustomAttribute<DescriptionAttribute>()?.Description);
            // Pause
            Console.ReadKey();
        }
    
        public static string GetPropertyAnotation(PropertyInfo propertyInfo, string element) {
            string assemblyPath = propertyInfo.DeclaringType.Assembly.Location;
            string xmlFilePath = System.IO.Path.ChangeExtension(assemblyPath, ".xml");
            XDocument xmlDoc = XDocument.Load(xmlFilePath);
            string propertyName = $"{propertyInfo.DeclaringType.FullName}.{propertyInfo.Name}";
            XElement memberElement = xmlDoc.Descendants("member")
                .FirstOrDefault(m => m.Attribute("name")?.Value == $"P:{propertyName}");
            string elementValue = memberElement?.Element(element)?.Value.Trim();
            return elementValue;
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments