• 使用Wesky.Net.Opentools库,一行代码实现自动解析实体类summary注释信息(可用于数据实体文档的快速实现)


    使用前,需要对你的项目勾选输出api文档文件。
    0
    引用Wesky.Net.OpenTools包,保持1.0.11版本或以上。
    0
     
    为了方便,我直接在昨天的演示基础上,继续给实体类添加注释。
    昨天的演示文章可参考:
    C#/.NET一行代码把实体类类型转换为Json数据字符串
    对实体类添加注释:
    0
    然后传入实体类型,即可获取到类型数据集合:
    0
    运行一下看下效果:
    0
    以上只是简单演示,你也可以用来快速生成实体类说明文档。例如通过反射,获取所有类型,然后进行代入,解析出每个类型里面的属性以及注释,直接就是你的一个演示文档了。
    解析部分核心代码:
     
    复制代码
      /// 
      /// 生成给定类型的所有属性的摘要信息列表,搜索所有相关XML文档。
      /// Generates a list of summary information for all properties of a given type, searching through all relevant XML documents.
      /// 
      /// 要分析的类型。The type to analyze.
      /// 处理属性路径时用于嵌套属性的前缀。Prefix for nested properties to handle property paths correctly.
      /// 摘要信息实体列表。A list of summary information entities.
      public static List GenerateEntitySummaries(Type type, string parentPrefix = "")
      {
          var summaryInfos = new List();
          IEnumerable<string> xmlPaths = GetAllXmlDocumentationPaths();
    
          foreach (string xmlPath in xmlPaths)
          {
              if (File.Exists(xmlPath))
              {
                  XDocument xmlDoc = XDocument.Load(xmlPath);
                  XElement root = xmlDoc.Root;
    
                  summaryInfos.AddRange(ExtractSummaryInfo(type, root, parentPrefix));
              }
          }
    
          return summaryInfos;
      }
    
      /// 
      /// 获取当前执行环境目录下所有XML文档的路径。
      /// Retrieves the paths to all XML documentation files in the current execution environment directory.
      /// 
      /// 所有XML文档文件的路径列表。A list of paths to all XML documentation files.
      private static IEnumerable<string> GetAllXmlDocumentationPaths()
      {
          string basePath = AppContext.BaseDirectory;
          return Directory.GetFiles(basePath, "*.xml", SearchOption.TopDirectoryOnly);
      }
    
      /// 
      /// 从XML文档中提取指定类型的所有属性的摘要信息。
      /// Extracts summary information for all properties of a specified type from an XML document.
      /// 
      /// 属性所属的类型。The type to which the properties belong.
      /// XML文档的根元素。The root element of the XML document.
      /// 属性的前缀路径。The prefix path for properties.
      /// 摘要信息实体列表。A list of summary information entities.
      private static List ExtractSummaryInfo(Type type, XElement root, string parentPrefix)
      {
          var infos = new List();
    
          foreach (PropertyInfo property in type.GetProperties())
          {
              string fullPath = string.IsNullOrEmpty(parentPrefix) ? property.Name : $"{parentPrefix}.{property.Name}";
              string typeName = property.PropertyType.Name;
    
              if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
              {
                  Type propertyType = property.PropertyType;
                  if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
                  {
                      propertyType = propertyType.GetGenericArguments()[0];
                      typeName = $"List<{propertyType.Name}>";
                  }
    
                  infos.AddRange(GenerateEntitySummaries(propertyType, fullPath));
              }
              else
              {
                  string summary = GetPropertySummary(root, type, property);
                  infos.Add(new DynamicSumaryInfo
                  {
                      Name = fullPath,
                      TypeName = typeName,
                      Summary = summary ?? string.Empty
                  });
              }
          }
    
          return infos;
      }
    复制代码

     

    OpenTools系列文章快捷链接【新版本完全兼容旧版本,不需要更新任何代码均可使用】:
    1.0.10版本:
    C#/.NET一行代码把实体类类型转换为Json数据字符串
    1.0.8版本:
    上位机和工控必备!用.NET快速搞定Modbus通信的方法
    1.0.7版本:
    大揭秘!.Net如何在5分钟内快速实现物联网扫码器通用扫码功能?
    1.0.6版本:
    .NET实现获取NTP服务器时间并同步(附带Windows系统启用NTP服务功能)
    1.0.5版本:
    C#使用P/Invoke来实现注册表的增删改查功能
    1.0.3版本:
    C#实现图片转Base64字符串,以及base64字符串在Markdown文件内复原的演示
    1.0.2版本:
    C#实现Ping远程主机功能(支持IP和域名)
    1.0.1版本:
    开始开源项目OpenTools的创作(第一个功能:AES加密解密)
     
    【备注】包版本完全开源,并且没有任何第三方依赖。使用.net framework 4.6+、任意其他跨平台.net版本环境,均可直接引用。
     
    再次感谢各位阅读~~~
     
     
  • 相关阅读:
    Python装饰器:套层壳我变得更强了!
    【从0-1成为架构师】性能优化的手段
    2022 12 3
    红日靶场五(vulnstack5)渗透分析
    免费的ssh工具
    Java自动装箱与自动拆箱
    Docker 部署前端项目(非自动化)
    力扣 -- 646. 最长数对链
    PreScan快速入门到精通第二十二讲驾驶员模型之路径跟踪
    MybatisPlus学习(二)---标准分页
  • 原文地址:https://www.cnblogs.com/weskynet/p/18236082