博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XmlDocumentationExtensions
阅读量:4498 次
发布时间:2019-06-08

本文共 6802 字,大约阅读时间需要 22 分钟。

// Reading XML Documentation at Run-Time// Bradley Smith - 2010/11/25using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Xml.Linq;using System.Xml.XPath;/// /// Provides extension methods for reading XML comments from reflected members./// public static class XmlDocumentationExtensions {    private static Dictionary
cachedXml; ///
/// Static constructor. /// static XmlDocumentationExtensions() { cachedXml = new Dictionary
(StringComparer.OrdinalIgnoreCase); } ///
/// Returns the expected name for a member element in the XML documentation file. /// ///
The reflected member. ///
The name of the member element.
private static string GetMemberElementName(MemberInfo member) { char prefixCode; string memberName = (member is Type) ? ((Type)member).FullName // member is a Type : (member.DeclaringType.FullName + "." + member.Name); // member belongs to a Type switch (member.MemberType) { case MemberTypes.Constructor: // XML documentation uses slightly different constructor names memberName = memberName.Replace(".ctor", "#ctor"); goto case MemberTypes.Method; case MemberTypes.Method: prefixCode = 'M'; // parameters are listed according to their type, not their name string paramTypesList = String.Join( ",", ((MethodBase)member).GetParameters() .Cast
() .Select(x => x.ParameterType.FullName ).ToArray() ); if (!String.IsNullOrEmpty(paramTypesList)) memberName += "(" + paramTypesList + ")"; break; case MemberTypes.Event: prefixCode = 'E'; break; case MemberTypes.Field: prefixCode = 'F'; break; case MemberTypes.NestedType: // XML documentation uses slightly different nested type names memberName = memberName.Replace('+', '.'); goto case MemberTypes.TypeInfo; case MemberTypes.TypeInfo: prefixCode = 'T'; break; case MemberTypes.Property: prefixCode = 'P'; break; default: throw new ArgumentException("Unknown member type", "member"); } // elements are of the form "M:Namespace.Class.Method" return String.Format("{0}:{1}", prefixCode, memberName); } ///
/// Returns the XML documentation (summary tag) for the specified member. /// ///
The reflected member. ///
The contents of the summary tag for the member.
public static string GetXmlDocumentation(this MemberInfo member) { AssemblyName assemblyName = member.Module.Assembly.GetName(); return GetXmlDocumentation(member, assemblyName.Name + ".xml"); } ///
/// Returns the XML documentation (summary tag) for the specified member. /// ///
The reflected member. ///
Path to the XML documentation file. ///
The contents of the summary tag for the member.
public static string GetXmlDocumentation(this MemberInfo member, string pathToXmlFile) { AssemblyName assemblyName = member.Module.Assembly.GetName(); XDocument xml = null; if (cachedXml.ContainsKey(assemblyName.FullName)) xml = cachedXml[assemblyName.FullName]; else cachedXml[assemblyName.FullName] = (xml = XDocument.Load(pathToXmlFile)); return GetXmlDocumentation(member, xml); } ///
/// Returns the XML documentation (summary tag) for the specified member. /// ///
The reflected member. ///
XML documentation. ///
The contents of the summary tag for the member.
public static string GetXmlDocumentation(this MemberInfo member, XDocument xml) { return xml.XPathEvaluate( String.Format( "string(/doc/members/member[@name='{0}']/summary)", GetMemberElementName(member) ) ).ToString().Trim(); } ///
/// Returns the XML documentation (returns/param tag) for the specified parameter. /// ///
The reflected parameter (or return value). ///
The contents of the returns/param tag for the parameter.
public static string GetXmlDocumentation(this ParameterInfo parameter) { AssemblyName assemblyName = parameter.Member.Module.Assembly.GetName(); return GetXmlDocumentation(parameter, assemblyName.Name + ".xml"); } ///
/// Returns the XML documentation (returns/param tag) for the specified parameter. /// ///
The reflected parameter (or return value). ///
Path to the XML documentation file. ///
The contents of the returns/param tag for the parameter.
public static string GetXmlDocumentation(this ParameterInfo parameter, string pathToXmlFile) { AssemblyName assemblyName = parameter.Member.Module.Assembly.GetName(); XDocument xml = null; if (cachedXml.ContainsKey(assemblyName.FullName)) xml = cachedXml[assemblyName.FullName]; else cachedXml[assemblyName.FullName] = (xml = XDocument.Load(pathToXmlFile)); return GetXmlDocumentation(parameter, xml); } ///
/// Returns the XML documentation (returns/param tag) for the specified parameter. /// ///
The reflected parameter (or return value). ///
XML documentation. ///
The contents of the returns/param tag for the parameter.
public static string GetXmlDocumentation(this ParameterInfo parameter, XDocument xml) { if (parameter.IsRetval || String.IsNullOrEmpty(parameter.Name)) return xml.XPathEvaluate( String.Format( "string(/doc/members/member[@name='{0}']/returns)", GetMemberElementName(parameter.Member) ) ).ToString().Trim(); else return xml.XPathEvaluate( String.Format( "string(/doc/members/member[@name='{0}']/param[@name='{1}'])", GetMemberElementName(parameter.Member), parameter.Name ) ).ToString().Trim(); }}

  

转载于:https://www.cnblogs.com/kevin1988/p/7084906.html

你可能感兴趣的文章
成熟稳重:成熟稳重
查看>>
WCF:目录
查看>>
获得两个整形二进制表达位数不同的数量
查看>>
动态创建div(鼠标放上显示二维码)
查看>>
社会化媒体投放猛增,哪个指标所受大家认可?
查看>>
64位电脑上配置mysql-connector-odbc的方法
查看>>
【转】Internet与Intranet区别
查看>>
【2019.1.24】 搜索,动规 经典题目体验赛
查看>>
script脚本中写不写$(document).ready(function() {});的区别
查看>>
每天CookBook之Python-065
查看>>
安装cocoa pods
查看>>
Discuz常用函数解析
查看>>
BZOJ1492: [NOI2007]货币兑换Cash
查看>>
Python网络编程
查看>>
poj 1847( floyd && spfa )
查看>>
SP10628 COT - Count on a tree 主席树
查看>>
[COI2007] Patrik 音乐会的等待 单调栈
查看>>
twoSum
查看>>
logstash之multiline插件,匹配多行日志
查看>>
SQL语句生成——界面操作
查看>>