Move some more UnmanagedCallersOnly tests to IL now that they're invalid C# (#43366)
authorJeremy Koritzinsky <jekoritz@microsoft.com>
Mon, 9 Nov 2020 18:35:33 +0000 (10:35 -0800)
committerGitHub <noreply@github.com>
Mon, 9 Nov 2020 18:35:33 +0000 (10:35 -0800)
src/tests/Interop/UnmanagedCallersOnly/InvalidCallbacks.il
src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.cs
src/tests/Interop/UnmanagedCallersOnly/UnmanagedCallersOnlyTest.csproj

index 9eac369..f85723a 100644 (file)
@@ -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 { }
 
index 1ebe513..93ab29a 100644 (file)
@@ -9,6 +9,7 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Threading;
 using TestLibrary;
+using InvalidCSharp;
 
 public class Program
 {
@@ -31,26 +32,6 @@ public class Program
         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 class Program
         // Local function to delay exception thrown during JIT
         void CallAsDelegate()
         {
-            Func<int, int> invoker = (Func<int, int>)GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("GetDoubleDelegate").Invoke(null, BindingFlags.DoNotWrapExceptions, null, null, null);
+            Func<int, int> invoker = CallingUnmanagedCallersOnlyDirectly.GetDoubleDelegate();
             invoker(0);
         }
     }
@@ -364,7 +345,7 @@ public class Program
            {
                 .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 @@ public class Program
         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 @@ public class Program
         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 @@ public class Program
         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 @@ public class Program
         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<int>).GetMethod("CallbackMethod"));
         il.Emit(OpCodes.Stloc_0);
         il.Emit(OpCodes.Ldloc_0);
 
@@ -718,11 +699,10 @@ public class Program
         Console.WriteLine($"Running {nameof(TestPInvokeMarkedWithUnmanagedCallersOnly)}...");
 
         // Call P/Invoke directly
-        var pInvokeWrapperMethod = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("CallPInvokeMarkedWithUnmanagedCallersOnly");
-        Assert.Throws<NotSupportedException>(() => pInvokeWrapperMethod.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null));
+        Assert.Throws<NotSupportedException>(() => CallingUnmanagedCallersOnlyDirectly.CallPInvokeMarkedWithUnmanagedCallersOnly(0));
 
         // Call P/Invoke via reflection
-        var method = GetCallingUnmanagedCallersOnlyDirectlyType().GetMethod("PInvokeMarkedWithUnmanagedCallersOnly");
+        var method = typeof(CallingUnmanagedCallersOnlyDirectly).GetMethod(nameof(CallingUnmanagedCallersOnlyDirectly.PInvokeMarkedWithUnmanagedCallersOnly));
         Assert.Throws<NotSupportedException>(() => method.Invoke(null, BindingFlags.DoNotWrapExceptions, null, new[] { (object)0 }, null));
 
         // Call P/Invoke as a function pointer
index 170fac3..72c30ff 100644 (file)
@@ -1,6 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>Exe</OutputType>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="UnmanagedCallersOnlyTest.cs" />