XmlDocumentationExtensions
Provides extension methods for reading XML comments from reflected members.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Xml.XPath;
namespace Autodesk.DesignScript.Runtime
{
public static class XmlDocumentationExtensions
{
private static Dictionary<string, XDocument> cachedXml;
static XmlDocumentationExtensions()
{
cachedXml = new Dictionary<string, XDocument>(StringComparer.OrdinalIgnoreCase);
}
private static string GetMemberElementName(MemberInfo member)
{
string text = (member is Type) ? ((Type)member).FullName : (member.DeclaringType.FullName + "." + member.Name);
char c;
switch (member.MemberType) {
case MemberTypes.Constructor:
text = text.Replace(".ctor", "#ctor");
goto case MemberTypes.Method;
case MemberTypes.Method: {
c = 'M';
string text2 = string.Join(",", (from ParameterInfo x in ((MethodBase)member).GetParameters()
select x.ParameterType.FullName).ToArray());
if (!string.IsNullOrEmpty(text2))
text = text + "(" + text2 + ")";
break;
}
case MemberTypes.Event:
c = 'E';
break;
case MemberTypes.Field:
c = 'F';
break;
case MemberTypes.NestedType:
text = text.Replace('+', '.');
goto case MemberTypes.TypeInfo;
case MemberTypes.TypeInfo:
c = 'T';
break;
case MemberTypes.Property:
c = 'P';
break;
default:
throw new ArgumentException("Unknown member type", "member");
}
return $"{c}""{text}";
}
public static string GetXmlDocumentation(this MemberInfo member)
{
AssemblyName name = member.Module.Assembly.GetName();
return member.GetXmlDocumentation(name.Name + ".xml");
}
public static IEnumerable<string> GetSearchTags(this MemberInfo member)
{
AssemblyName name = member.Module.Assembly.GetName();
return member.GetSearchTags(name.Name + ".xml");
}
public static IEnumerable<string> GetSearchTags(this MemberInfo member, string pathToXmlFile)
{
AssemblyName name = member.Module.Assembly.GetName();
XDocument xDocument = null;
xDocument = (cachedXml.ContainsKey(name.FullName) ? cachedXml[name.FullName] : (cachedXml[name.FullName] = XDocument.Load(pathToXmlFile)));
return member.GetSearchTags(xDocument);
}
public static IEnumerable<string> GetSearchTags(this MemberInfo member, XDocument xml)
{
if (xml == null)
return new List<string>();
return from x in xml.XPathEvaluate($"""{GetMemberElementName(member)}""").ToString().Split(new char[1] {
','
})
select x.Trim() into x
where x != string.Empty
select x;
}
public static string GetXmlDocumentation(this MemberInfo member, string pathToXmlFile)
{
AssemblyName name = member.Module.Assembly.GetName();
XDocument xDocument = null;
xDocument = (cachedXml.ContainsKey(name.FullName) ? cachedXml[name.FullName] : (cachedXml[name.FullName] = XDocument.Load(pathToXmlFile)));
return member.GetXmlDocumentation(xDocument);
}
public static string GetXmlDocumentation(this MemberInfo member, XDocument xml)
{
return xml.XPathEvaluate($"""{GetMemberElementName(member)}""").ToString().Trim();
}
public static string GetXmlDocumentation(this ParameterInfo parameter)
{
AssemblyName name = parameter.Member.Module.Assembly.GetName();
return parameter.GetXmlDocumentation(name.Name + ".xml");
}
public static string GetXmlDocumentation(this ParameterInfo parameter, string pathToXmlFile)
{
AssemblyName name = parameter.Member.Module.Assembly.GetName();
XDocument xDocument = null;
xDocument = (cachedXml.ContainsKey(name.FullName) ? cachedXml[name.FullName] : (cachedXml[name.FullName] = XDocument.Load(pathToXmlFile)));
return parameter.GetXmlDocumentation(xDocument);
}
public static string GetXmlDocumentation(this ParameterInfo parameter, XDocument xml)
{
if (parameter.IsRetval || string.IsNullOrEmpty(parameter.Name))
return xml.XPathEvaluate($"""{GetMemberElementName(parameter.Member)}""").ToString().Trim();
return xml.XPathEvaluate($"""{GetMemberElementName(parameter.Member)}""{parameter.Name}""").ToString().Trim();
}
}
}