CachingXDocument
Caching layer hiding the details of accessing DLL documentation.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
namespace Namotion.Reflection
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class CachingXDocument
{
private static readonly XName XNameDoc = "doc";
private static readonly XName XNameMembers = "members";
private static readonly XName XNameMember = "member";
private static readonly XName XNameName = "name";
private readonly object _lock = new object();
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
1,
2
})]
private readonly Dictionary<string, XElement> _elementByNameCache = new Dictionary<string, XElement>();
private readonly XDocument _document;
[System.Runtime.CompilerServices.NullableContext(2)]
internal CachingXDocument(string pathToXmlFile)
{
XDocument xDocument = _document = XDocument.Load(pathToXmlFile, LoadOptions.PreserveWhitespace);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
internal XElement GetXmlDocsElement(string name)
{
lock (_lock) {
if (!_elementByNameCache.TryGetValue(name, out XElement value)) {
value = GetXmlDocsElement(_document, name);
_elementByNameCache[name] = value;
}
return value;
}
}
[return: System.Runtime.CompilerServices.Nullable(2)]
internal static XElement GetXmlDocsElement(XDocument document, string name)
{
foreach (XElement item in document.Element(XNameDoc).Element(XNameMembers).Elements(XNameMember)) {
if (item.Attribute(XNameName)?.Value == name)
return item;
}
return null;
}
}
}