Skip to content

Commit d71b334

Browse files
committed
Convert XmlKeyHelper to use UnsafeAccessorType
1 parent fe47df6 commit d71b334

File tree

1 file changed

+29
-36
lines changed
  • src/libraries/System.Security.Cryptography/src/System/Security/Cryptography

1 file changed

+29
-36
lines changed

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/XmlKeyHelper.cs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections;
66
using System.Diagnostics;
77
using System.Diagnostics.CodeAnalysis;
8-
using System.Reflection;
8+
using System.Runtime.CompilerServices;
99
using System.Text;
1010

1111
namespace System.Security.Cryptography
@@ -259,51 +259,44 @@ internal bool HasElement(string localName)
259259
private static class Functions
260260
{
261261
private const string XmlLinqAssemblyString = ", System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51";
262+
private const string XDocumentTypeName = "System.Xml.Linq.XDocument" + XmlLinqAssemblyString;
263+
private const string XContainerTypeName = "System.Xml.Linq.XContainer" + XmlLinqAssemblyString;
264+
private const string XElementTypeName = "System.Xml.Linq.XElement" + XmlLinqAssemblyString;
265+
private const string XNameTypeName = "System.Xml.Linq.XName" + XmlLinqAssemblyString;
262266

263-
private static readonly Func<string, object> s_xDocumentCreate;
264-
private static readonly PropertyInfo s_docRootProperty;
265-
private static readonly MethodInfo s_getElementsMethod;
266-
private static readonly PropertyInfo s_elementNameProperty;
267-
private static readonly PropertyInfo s_elementValueProperty;
268-
private static readonly PropertyInfo s_nameNameProperty;
267+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "Parse")]
268+
[return: UnsafeAccessorType(XDocumentTypeName)]
269+
private static extern object XDocument_Parse(
270+
[UnsafeAccessorType(XDocumentTypeName)] object?_, string xmlString);
269271

270-
#pragma warning disable CA1810 // explicit static cctor
271-
static Functions()
272-
{
273-
Type xDocument = Type.GetType("System.Xml.Linq.XDocument" + XmlLinqAssemblyString)!;
274-
s_xDocumentCreate = xDocument.GetMethod(
275-
"Parse",
276-
BindingFlags.Static | BindingFlags.Public,
277-
new[] { typeof(string) })!
278-
.CreateDelegate<Func<string, object>>();
279-
280-
s_docRootProperty = xDocument.GetProperty("Root")!;
281-
282-
Type xElement = Type.GetType("System.Xml.Linq.XElement" + XmlLinqAssemblyString)!;
283-
s_getElementsMethod = xElement.GetMethod(
284-
"Elements",
285-
BindingFlags.Instance | BindingFlags.Public,
286-
Type.EmptyTypes)!;
287-
288-
s_elementNameProperty = xElement.GetProperty("Name")!;
289-
s_elementValueProperty = xElement.GetProperty("Value")!;
290-
291-
Type xName = Type.GetType("System.Xml.Linq.XName" + XmlLinqAssemblyString)!;
292-
s_nameNameProperty = xName.GetProperty("LocalName")!;
293-
}
294-
#pragma warning restore CA1810
272+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Root")]
273+
[return: UnsafeAccessorType(XElementTypeName)]
274+
private static extern object? XDocument_GetRoot([UnsafeAccessorType(XDocumentTypeName)] object xDocument);
275+
276+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "Elements")]
277+
private static extern IEnumerable XContainer_Elements([UnsafeAccessorType(XContainerTypeName)] object xElement);
278+
279+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Name")]
280+
[return: UnsafeAccessorType(XNameTypeName)]
281+
private static extern object XElement_GetName([UnsafeAccessorType(XElementTypeName)] object xElement);
282+
283+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Value")]
284+
private static extern string? XElement_GetValue([UnsafeAccessorType(XElementTypeName)] object xElement);
285+
286+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_LocalName")]
287+
private static extern string? XName_GetLocalName([UnsafeAccessorType(XNameTypeName)] object xName);
295288

296289
internal static object? ParseDocument(string xmlString) =>
297-
s_docRootProperty.GetValue(s_xDocumentCreate(xmlString));
290+
XDocument_GetRoot(XDocument_Parse(null, xmlString));
298291

299292
internal static IEnumerable? GetElements(object? element) =>
300-
(IEnumerable?)s_getElementsMethod.Invoke(element, Array.Empty<object>());
293+
XContainer_Elements(element!);
301294

302295
internal static string? GetLocalName(object? element) =>
303-
(string?)s_nameNameProperty.GetValue(s_elementNameProperty.GetValue(element));
296+
XName_GetLocalName(XElement_GetName(element!));
304297

305298
internal static string? GetValue(object? element) =>
306-
(string?)s_elementValueProperty.GetValue(element);
299+
XElement_GetValue(element!);
307300
}
308301
}
309302
}

0 commit comments

Comments
 (0)