diff --git a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs index 467ba639e79989..fa87cf9c7bb214 100644 --- a/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs +++ b/src/libraries/Common/src/System/Runtime/Serialization/SerializationGuard.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Reflection; +using System.Runtime.CompilerServices; namespace System.Runtime.Serialization { @@ -10,34 +11,16 @@ namespace System.Runtime.Serialization /// internal static partial class SerializationGuard { - private delegate void ThrowIfDeserializationInProgressWithSwitchDel(string switchName, ref int cachedValue); - private static readonly ThrowIfDeserializationInProgressWithSwitchDel? s_throwIfDeserializationInProgressWithSwitch = CreateThrowIfDeserializationInProgressWithSwitchDelegate(); - - /// - /// Builds a wrapper delegate for SerializationInfo.ThrowIfDeserializationInProgress(string, ref int), - /// since it is not exposed via contracts. - /// - private static ThrowIfDeserializationInProgressWithSwitchDel? CreateThrowIfDeserializationInProgressWithSwitchDelegate() - { - ThrowIfDeserializationInProgressWithSwitchDel? throwIfDeserializationInProgressDelegate = null; - MethodInfo? throwMethod = typeof(SerializationInfo).GetMethod("ThrowIfDeserializationInProgress", - BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(string), typeof(int).MakeByRefType() }, Array.Empty()); - - if (throwMethod != null) - { - throwIfDeserializationInProgressDelegate = throwMethod.CreateDelegate(); - } - - return throwIfDeserializationInProgressDelegate; - } - /// /// Provides access to the internal "ThrowIfDeserializationInProgress" method on . /// No-ops if the Serialization Guard feature is disabled or unavailable. /// public static void ThrowIfDeserializationInProgress(string switchSuffix, ref int cachedValue) { - s_throwIfDeserializationInProgressWithSwitch?.Invoke(switchSuffix, ref cachedValue); + ThrowIfDeserializationInProgress(null, switchSuffix, ref cachedValue); + + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ThrowIfDeserializationInProgress")] + static extern void ThrowIfDeserializationInProgress(SerializationInfo? thisPtr, string switchSuffix, ref int cachedValue); } } }