diff --git a/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il b/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il index 9eac36900a7998..f85723a9fcaa0f 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il +++ b/src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il @@ -1,8 +1,14 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -.assembly extern System.Runtime { } -.assembly extern System.Runtime.InteropServices { } +.assembly extern System.Runtime +{ + .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) +} +.assembly extern System.Runtime.InteropServices +{ + .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) +} .assembly InvalidCSharp { } diff --git a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs index 1ebe513e4566ca..93ab29a5c1ea7a 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs +++ b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices; using System.Threading; using TestLibrary; +using InvalidCSharp; public class Program { @@ -31,26 +32,6 @@ public static class UnmanagedCallersOnlyDll public static extern int CallManagedProcCatchException(IntPtr callbackProc, int n); } - private const string InvalidCSharpAssemblyName = "InvalidCSharp"; - - public static Type GetCallbacksType() - { - var asm = Assembly.Load(InvalidCSharpAssemblyName); - return asm.GetType("InvalidCSharp.Callbacks"); - } - - public static Type GetGenericClassOfIntType() - { - var asm = Assembly.Load(InvalidCSharpAssemblyName); - return asm.GetType("InvalidCSharp.GenericClass`1").MakeGenericType(typeof(int)); - } - - public static Type GetCallingUnmanagedCallersOnlyDirectlyType() - { - var asm = Assembly.Load(InvalidCSharpAssemblyName); - return asm.GetType("InvalidCSharp.CallingUnmanagedCallersOnlyDirectly"); - } - private delegate int IntNativeMethodInvoker(); private delegate void NativeMethodInvoker(); @@ -349,7 +330,7 @@ public static void NegativeTest_ViaDelegate() // Local function to delay exception thrown during JIT void CallAsDelegate() { - Func invoker = (Func)GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("GetDoubleDelegate").Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null); + Func invoker = CallingUnmanagedCallersOnlyDirectly.GetDoubleDelegate(); invoker(0); } } @@ -364,7 +345,7 @@ void TestUnmanagedCallersOnlyNonStatic() { .locals init ([0] native int ptr) nop - ldftn int GetCallbacksType().CallbackNonStatic(int) + ldftn int typeof(Callbacks).CallbackNonStatic(int) stloc.0 ldloc.0 @@ -381,7 +362,7 @@ ldftn int GetCallbacksType().CallbackNonStatic(int) il.Emit(OpCodes.Nop); // Get native function pointer of the callback - il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackNonStatic")); + il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackNonStatic")); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); @@ -466,7 +447,7 @@ .locals init ([0] native int ptr) il.Emit(OpCodes.Nop); // Get native function pointer of the callback - il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackMethodGeneric")); + il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackMethodGeneric")); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ret); @@ -502,7 +483,7 @@ ldftn void InvalidCSharp.Callbacks.CallbackMethodGeneric(int) il.Emit(OpCodes.Nop); // Get native function pointer of the instantiated generic callback - il.Emit(OpCodes.Ldftn, GetCallbacksType().GetMethod("CallbackMethodGeneric").MakeGenericMethod(new [] { typeof(int) })); + il.Emit(OpCodes.Ldftn, typeof(Callbacks).GetMethod("CallbackMethodGeneric").MakeGenericMethod(new [] { typeof(int) })); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); @@ -544,7 +525,7 @@ .locals init ([0] native int ptr) il.Emit(OpCodes.Nop); // Get native function pointer of the callback from the instantiated generic class. - il.Emit(OpCodes.Ldftn, GetGenericClassOfIntType().GetMethod("CallbackMethod")); + il.Emit(OpCodes.Ldftn, typeof(GenericClass).GetMethod("CallbackMethod")); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); @@ -718,11 +699,10 @@ public static void TestPInvokeMarkedWithUnmanagedCallersOnly() Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}..."); // Call P/Invoke directly - var pInvokeWrapperMethod = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("CallPInvokeMarkedWithUnmanagedCallersOnly"); - Assert.Throws(() => pInvokeWrapperMethod.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null)); + Assert.Throws(() => CallingUnmanagedCallersOnlyDirectly.CallPInvokeMarkedWithUnmanagedCallersOnly(0)); // Call P/Invoke via reflection - var method = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("PInvokeMarkedWithUnmanagedCallersOnly"); + var method = typeof(CallingUnmanagedCallersOnlyDirectly).GetMethod(nameof(CallingUnmanagedCallersOnlyDirectly.PInvokeMarkedWithUnmanagedCallersOnly)); Assert.Throws(() => method.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null)); // Call P/Invoke as a function pointer diff --git a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj index 170fac3899124b..72c30ff8cdb7ed 100644 --- a/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj +++ b/src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj @@ -1,6 +1,7 @@ Exe + true