using System; using Namotion.Reflection; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string studentSummary = typeof(Student).GetXmlDocsSummary(); Console.WriteLine(studentSummary); var piList = typeof(Student).GetProperties(); foreach (var pi in piList) { var summary = pi.GetXmlDocsSummary(); Console.WriteLine($"{pi.Name}-->{summary}"); } } } /// <summary> /// 学生信息 /// </summary> public class Student { /// <summary> /// Id /// </summary> public int Id { get; set; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 性别 /// </summary> public string Sex { get; set; } } }
使用组件Namotion.Reflection,同时需要在项目属性中开启生成xml启动项
运行结果:
学生信息 Id-->Id Name-->姓名 Sex-->性别
获取字段中的summary
internal static class SummaryView { internal static string GetSummary(this object obj) { var type = obj.GetType(); foreach (var item in type.GetFields()) { //找到对应的字段 if (item.GetValue(obj).Equals(obj)) { return item.GetXmlDocsSummary(); }; } return null; } }
使用
FirstEmnu emnu = FirstEmnu.deleted;
Console.WriteLine(emnu.GetSummary());