|  | 
| 5 | 5 | using System.Collections; | 
| 6 | 6 | using System.Diagnostics; | 
| 7 | 7 | using System.Diagnostics.CodeAnalysis; | 
| 8 |  | -using System.Reflection; | 
|  | 8 | +using System.Runtime.CompilerServices; | 
| 9 | 9 | using System.Text; | 
| 10 | 10 | 
 | 
| 11 | 11 | namespace System.Security.Cryptography | 
| @@ -259,51 +259,44 @@ internal bool HasElement(string localName) | 
| 259 | 259 |             private static class Functions | 
| 260 | 260 |             { | 
| 261 | 261 |                 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; | 
| 262 | 266 | 
 | 
| 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); | 
| 269 | 271 | 
 | 
| 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); | 
| 295 | 288 | 
 | 
| 296 | 289 |                 internal static object? ParseDocument(string xmlString) => | 
| 297 |  | -                    s_docRootProperty.GetValue(s_xDocumentCreate(xmlString)); | 
|  | 290 | +                    XDocument_GetRoot(XDocument_Parse(null, xmlString)); | 
| 298 | 291 | 
 | 
| 299 | 292 |                 internal static IEnumerable? GetElements(object? element) => | 
| 300 |  | -                    (IEnumerable?)s_getElementsMethod.Invoke(element, Array.Empty<object>()); | 
|  | 293 | +                    XContainer_Elements(element!); | 
| 301 | 294 | 
 | 
| 302 | 295 |                 internal static string? GetLocalName(object? element) => | 
| 303 |  | -                    (string?)s_nameNameProperty.GetValue(s_elementNameProperty.GetValue(element)); | 
|  | 296 | +                    XName_GetLocalName(XElement_GetName(element!)); | 
| 304 | 297 | 
 | 
| 305 | 298 |                 internal static string? GetValue(object? element) => | 
| 306 |  | -                    (string?)s_elementValueProperty.GetValue(element); | 
|  | 299 | +                    XElement_GetValue(element!); | 
| 307 | 300 |             } | 
| 308 | 301 |         } | 
| 309 | 302 |     } | 
|  | 
0 commit comments