前言:下午有小伙伴要求,讓我繼續做個解析實體類注釋信息的內容。所以我也順便加入進來。以下開始正文實戰操作:
項目需要勾選輸出api文檔文件。這樣就可以讓所有實體類的summary信息被寫入到輸出目錄下。如果有多個xml文件也沒關系,下面的包已經實現自動解析多個xml文件功能,只選取匹配的那個。
要引用 Wesky.Net.OpenTools?包,保持1.0.11版本或以上
為了方便,我直接在昨天的演示基礎上,繼續給實體類添加注釋。
昨天的演示文章可參考:
C#/.NET一行代碼把實體類類型轉換為Json數據字符串
https://mp.weixin.qq.com/s/nVcURD0lf5-AQOVzwHqcxw
對實體類添加注釋,該實體類嵌套一層集合屬性。
然后傳入實體類型,即可獲取到類型數據集合:
運行一下看下效果:
以上只是簡單演示,你也可以用來快速生成實體類說明文檔。例如:通過反射,獲取所有類型,然后進行代入,解析出每個類型里面的屬性以及注釋,直接就是你的一個實體說明文檔了。
解析部分核心代碼:
/// <summary>/// 生成給定類型的所有屬性的摘要信息列表,搜索所有相關XML文檔。/// Generates a list of summary information for all properties of a given type, searching through all relevant XML documents./// </summary>/// <param name="type">要分析的類型。The type to analyze.</param>/// <param name="parentPrefix">處理屬性路徑時用于嵌套屬性的前綴。Prefix for nested properties to handle property paths correctly.</param>/// <returns>摘要信息實體列表。A list of summary information entities.</returns>public static List<DynamicSumaryInfo> GenerateEntitySummaries(Type type, string parentPrefix = ""){var summaryInfos = new List<DynamicSumaryInfo>();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;}/// <summary>/// 獲取當前執行環境目錄下所有XML文檔的路徑。/// Retrieves the paths to all XML documentation files in the current execution environment directory./// </summary>/// <returns>所有XML文檔文件的路徑列表。A list of paths to all XML documentation files.</returns>private static IEnumerable<string> GetAllXmlDocumentationPaths(){string basePath = AppContext.BaseDirectory;return Directory.GetFiles(basePath, "*.xml", SearchOption.TopDirectoryOnly);}/// <summary>/// 從XML文檔中提取指定類型的所有屬性的摘要信息。/// Extracts summary information for all properties of a specified type from an XML document./// </summary>/// <param name="type">屬性所屬的類型。The type to which the properties belong.</param>/// <param name="root">XML文檔的根元素。The root element of the XML document.</param>/// <param name="parentPrefix">屬性的前綴路徑。The prefix path for properties.</param>/// <returns>摘要信息實體列表。A list of summary information entities.</returns>private static List<DynamicSumaryInfo> ExtractSummaryInfo(Type type, XElement root, string parentPrefix){var infos = new List<DynamicSumaryInfo>();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;}
如果以上內容對你有幫助,歡迎點贊、留言、在看、轉發,也歡迎關注我的個人公眾號:【Dotnet Dancer】
快捷關注:
OpenTools系列文章快捷鏈接【新版本完全兼容舊版本,不需要更新任何代碼均可使用】:
1.0.10版本:
C#/.NET一行代碼把實體類類型轉換為Json數據字符串
https://mp.weixin.qq.com/s/nVcURD0lf5-AQOVzwHqcxw
1.0.8版本:
上位機和工控必備!用.NET快速搞定Modbus通信的方法
https://mp.weixin.qq.com/s/Yq6kuXzFglHfNUqrHcQO9w
1.0.7版本:
大揭秘!.Net如何在5分鐘內快速實現物聯網掃碼器通用掃碼功能?
https://mp.weixin.qq.com/s/-5VuLAS6HlElgDQXRY9-BQ
1.0.6版本:
.NET實現獲取NTP服務器時間并同步(附帶Windows系統啟用NTP服務功能)
https://mp.weixin.qq.com/s/vMW0vYC-D9z0Dp6HFSBqyg
1.0.5版本:
C#使用P/Invoke來實現注冊表的增刪改查功能
https://mp.weixin.qq.com/s/LpsjBhDDzkwyLU_tIpF-lg
1.0.3版本:
C#實現圖片轉Base64字符串,以及base64字符串在Markdown文件內復原的演示
https://mp.weixin.qq.com/s/n9VtTCIiVUbHJk7OfoCcvA
1.0.2版本:
C#實現Ping遠程主機功能(支持IP和域名)
https://mp.weixin.qq.com/s/d-2HcIM1KaLo-FrrTLkwEw
1.0.1版本:
開始開源項目OpenTools的創作(第一個功能:AES加密解密)
https://mp.weixin.qq.com/s/78TA-m?st?459AuvAHwQViqQ
【備注】包版本完全開源,并且沒有任何第三方依賴。使用.net framework 4.6+、任意其他跨平臺.net版本環境,均可直接引用。
再次感謝各位閱讀~~~