当前位置:
首页 > temp > 简明python教程 >
-
C# get class and method summary
/// <summary>
///Class Summary, Xml Comments Summary
/// </summary>
public class XmlCommentsSummary
{
/// <summary>
/// Print DateTime Now
/// </summary>
public void PrintTime()
{
Console.WriteLine($"Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
}
/// <summary>
/// Print random guid
/// </summary>
public void PrintGuid()
{
Console.WriteLine($"Guid is {Guid.NewGuid().ToString()}");
}
}
1.via OOS Namotion.Reflection
using Namotion.Reflection; static void SummaryDemo() { Type type = typeof(XmlCommentsSummary); string summary = type.GetXmlDocsSummary(); Console.WriteLine(summary); MethodInfo[] mis = type.GetMethods(); if(mis!=null && mis.Any()) { foreach(var mi in mis) { string sum = mi.GetXmlDocsSummary(); if(!string.IsNullOrWhiteSpace(sum)) { Console.WriteLine($"Method Name:{mi.Name},summary:{sum}"); } } } }
2.Copy stackoverflow solution from https://stackoverflow.com/questions/15602606/programmatically-get-summary-comments-at-runtime
/// <summary> /// Utility class to provide documentation for various types where available with the assembly /// </summary> public static class DocumenationExtensions { /// <summary> /// Provides the documentation comments for a specific method /// </summary> /// <param name="methodInfo">The MethodInfo (reflection data ) of the member to find documentation for</param> /// <returns>The XML fragment describing the method</returns> public static XmlElement GetDocumentation(this MethodInfo methodInfo) { // Calculate the parameter string as this is in the member name in the XML var parametersString = ""; foreach (var parameterInfo in methodInfo.GetParameters()) { if (parametersString.Length > 0) { parametersString += ","; } parametersString += parameterInfo.ParameterType.FullName; } //AL: 15.04.2008 ==> BUG-FIX remove “()” if parametersString is empty if (parametersString.Length > 0) { return XmlFromName(methodInfo.DeclaringType, 'M', methodInfo.Name + "(" + parametersString + ")"); } else { return XmlFromName(methodInfo.DeclaringType, 'M', methodInfo.Name); } } /// <summary> /// Provides the documentation comments for a specific member /// </summary> /// <param name="memberInfo">The MemberInfo (reflection data) or the member to find documentation for</param> /// <returns>The XML fragment describing the member</returns> public static XmlElement GetDocumentation(this MemberInfo memberInfo) { if (memberInfo != null) { // First character [0] of member type is prefix character in the name in the XML return XmlFromName(memberInfo.DeclaringType, memberInfo.MemberType.ToString()[0], memberInfo.Name); } return null; } /// <summary> /// Returns the Xml documenation summary comment for this member /// </summary> /// <param name="memberInfo"></param> /// <returns></returns> public static string GetSummary(this MemberInfo memberInfo) { if (memberInfo != null) { var element = memberInfo.GetDocumentation(); var summaryElm = element?.SelectSingleNode("summary"); if (summaryElm == null) { return ""; } return summaryElm.InnerText.Trim(); } return null; } /// <summary> /// Provides the documentation comments for a specific type /// </summary> /// <param name="type">Type to find the documentation for</param> /// <returns>The XML fragment that describes the type</returns> public static XmlElement GetDocumentation(this Type type) { // Prefix in type names is T return XmlFromName(type, 'T', ""); } /// <summary> /// Gets the summary portion of a type's documenation or returns an empty string if not available /// </summary> /// <param name="type"></param> /// <returns></returns> public static string GetSummary(this Type type) { var element = type.GetDocumentation(); var summaryElm = element?.SelectSingleNode("summary"); if (summaryElm == null) { return ""; } return summaryElm.InnerText.Trim(); } /// <summary> /// Obtains the XML Element that describes a reflection element by searching the /// members for a member that has a name that describes the element. /// </summary> /// <param name="type">The type or parent type, used to fetch the assembly</param> /// <param name="prefix">The prefix as seen in the name attribute in the documentation XML</param> /// <param name="name">Where relevant, the full name qualifier for the element</param> /// <returns>The member that has a name that describes the specified reflection element</returns> private static XmlElement XmlFromName(this Type type, char prefix, string name) { string fullName; if (string.IsNullOrEmpty(name)) { fullName = prefix + ":" + type.FullName; } else { fullName = prefix + ":" + type.FullName + "." + name; } var xmlDoc = XmlFromAssembly(type.Assembly); if (xmlDoc != null) { var matchedElement = xmlDoc["doc"]["members"].SelectSingleNode("member[@name='" + fullName + "']") as XmlElement; return matchedElement; } return null; } /// <summary> /// A cache used to remember Xml documentation for assemblies /// </summary> private static readonly Dictionary<Assembly, XmlDocument> Cache = new Dictionary<Assembly, XmlDocument>(); /// <summary> /// A cache used to store failure exceptions for assembly lookups /// </summary> private static readonly Dictionary<Assembly, Exception> FailCache = new Dictionary<Assembly, Exception>(); /// <summary> /// Obtains the documentation file for the specified assembly /// </summary> /// <param name="assembly">The assembly to find the XML document for</param> /// <returns>The XML document</returns> /// <remarks>This version uses a cache to preserve the assemblies, so that /// the XML file is not loaded and parsed on every single lookup</remarks> public static XmlDocument XmlFromAssembly(this Assembly assembly) { if (FailCache.ContainsKey(assembly)) { return null; } try { if (!Cache.ContainsKey(assembly)) { // load the docuemnt into the cache Cache[assembly] = XmlFromAssemblyNonCached(assembly); } return Cache[assembly]; } catch (Exception exception) { FailCache[assembly] = exception; return null; } } /// <summary> /// Loads and parses the documentation file for the specified assembly /// </summary> /// <param name="assembly">The assembly to find the XML document for</param> /// <returns>The XML document</returns> private static XmlDocument XmlFromAssemblyNonCached(Assembly assembly) { var assemblyFilename = assembly.CodeBase; const string prefix = "file:///"; if (assemblyFilename.StartsWith(prefix)) { StreamReader streamReader; try { streamReader = new StreamReader(Path.ChangeExtension(assemblyFilename.Substring(prefix.Length), ".xml")); } catch (FileNotFoundException exception) { throw new Exception("XML documentation not present (make sure it is turned on in project properties when building)", exception); } var xmlDocument = new XmlDocument(); xmlDocument.Load(streamReader); return xmlDocument; } else { throw new Exception("Could not ascertain assembly filename", null); } } } static void GetXmlSummaryComments() { Type type = typeof(XmlCommentsSummary); string typeSummary = type.GetSummary(); Console.WriteLine($"{typeSummary}"); MethodInfo[] mis = type.GetMethods(); if (mis != null && mis.Any()) { foreach (var mi in mis) { string methodSummary = type.GetMethod(mi.Name).GetSummary(); if(!string.IsNullOrWhiteSpace(methodSummary)) { Console.WriteLine($"Method Name:{mi.Name},summary:{methodSummary}"); } } } }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数