2020年5月7日星期四

C# get class and method summary

C# get class and method summary


/// <summary>
///Class Summary, /// </summary>
public class {
/// <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(string summary = type.Get= type.GetMethods();   if(mis!=null && mis.Any())   {    foreach(var mi in mis)    {     string sum = mi.Getif(!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 </returns>  public static this MethodInfo methodInfo)  {   // Calculate the parameter string as this is in the member name in the    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 'M', methodInfo.Name + "(" + parametersString + ")");   }   else   {    return '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 </returns>  public static this MemberInfo memberInfo)  {   if (memberInfo != null)   {    // First character [0] of member type is prefix character in the name in the     return 0], memberInfo.Name);   }   return null;  }  /// <summary>  /// Returns the /// </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 </returns>  public static this Type type)  {   // Prefix in type names is T   return '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 /// 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 </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 this Type type, char prefix, string name)  {   string fullName;   if (string.IsNullOrEmpty(name))   {    fullName = prefix + ":" + type.FullName;   }   else   {    fullName = prefix + ":" + type.FullName + "." + name;   }   var  if (null)   {    var matchedElement = "doc"]["members"].SelectSingleNode("member[@name='" + fullName + "']") as return matchedElement;   }   return null;  }  /// <summary>  /// A cache used to remember /// </summary>  private static readonly Dictionary<Assembly, new Dictionary<Assembly, ();  /// <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 </param>  /// <returns>The </returns>  /// <remarks>This version uses a cache to preserve the assemblies, so that   /// the </remarks>  public static this Assembly assembly)  {   if (FailCache.ContainsKey(assembly))   {    return null;   }   try   {    if (!Cache.ContainsKey(assembly))    {     // load the docuemnt into the cache     Cache[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 </param>  /// <returns>The </returns>  private static var assemblyFilename = assembly.CodeBase;   const string prefix = "file:///";   if (assemblyFilename.StartsWith(prefix))   {    StreamReader streamReader;    try    {     streamReader = new StreamReader(Path.ChangeExtension(assemblyFilename.Substring(prefix.Length), "."));    }    catch (FileNotFoundException exception)    {     throw new Exception("", exception);    }    var new return else   {    throw new Exception("Could not ascertain assembly filename", null);   }  } }static void Get= typeof(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}");     }     }   }   }

 


没有评论:

发表评论