123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Xml.Linq;
- using System.Xml.XPath;
- namespace EfDbCommentGenerator.Core
- {
- internal class XmlDocFinder
- {
- private readonly Dictionary<string, XDocument> _dicXml;
- public XmlDocFinder()
- {
- _dicXml = new Dictionary<string, XDocument>();
- }
- private XDocument GetXml(Assembly assembly)
- {
- var xmlFilePath = Path.ChangeExtension(assembly.Location, "xml");
- if (_dicXml.ContainsKey(xmlFilePath)) return _dicXml[xmlFilePath];
- XDocument load = null;
- try
- {
- load = XDocument.Load(xmlFilePath);
- }
- catch (Exception ex)
- {
- Debug.Print(ex.ToString());
- }
- return _dicXml[xmlFilePath] = load;
- }
- public string FindDoc(MemberInfo member)
- {
- var xml = GetXml(member.Module.Assembly);
- if (xml == null) return null;
- Type identityType;
- switch (member.MemberType)
- {
- case MemberTypes.TypeInfo:
- case MemberTypes.NestedType:
- return xml.XPathSelectElement($"/doc/members/member[@name=\"T:{((Type)member).FullName.Replace("+", ".")}\"]/summary")?.Value.Trim();
- case MemberTypes.Field:
- var fi = (FieldInfo)member;
- identityType = fi.ReflectedType;
- if (identityType != null && identityType.IsGenericType && false == identityType.IsGenericTypeDefinition)
- {
- identityType = identityType.GetGenericTypeDefinition();
- }
- Debug.Assert(identityType != null, "t != null");
- return xml.XPathSelectElement($"/doc/members/member[@name=\"F:{identityType.FullName.Replace("+", ".")}.{fi.Name}\"]/summary")?.Value.Trim();
- case MemberTypes.Property:
- var pi = (PropertyInfo)member;
- if (pi.ReflectedType != pi.DeclaringType && pi.DeclaringType != null)
- {
- var findDoc = FindDoc(pi.DeclaringType.GetProperty(pi.Name));
- return findDoc;
- }
- string additionalComment = null;
- if (pi.PropertyType.IsEnum)
- {
- additionalComment += FindDoc(pi.PropertyType)
- + "("
- + string.Join(",", Enum.GetValues(pi.PropertyType).Cast<object>().Select(p => pi.PropertyType.GetField(p.ToString())).Select(p => Convert.ToInt64(p.GetRawConstantValue()) + ":" + FindDoc(p)))
- + ")";
- }
- identityType = pi.ReflectedType;
- if (identityType != null && identityType.IsGenericType && false == identityType.IsGenericTypeDefinition)
- {
- identityType = identityType.GetGenericTypeDefinition();
- }
- Debug.Assert(identityType != null, "t != null");
- return xml.XPathSelectElement($"/doc/members/member[@name=\"P:{identityType.FullName.Replace("+", ".")}.{pi.Name}\"]/summary")?.Value.Trim()
- + additionalComment;
- case MemberTypes.Constructor:
- case MemberTypes.Event:
- case MemberTypes.Method:
- case MemberTypes.Custom:
- case MemberTypes.All:
- default:
- return null;
- }
- }
- }
- }
|