using System.Runtime.InteropServices;
using System.Threading;
using TestLibrary;
+using InvalidCSharp;
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();
// 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);
}
}
{
.locals init ([0] native int ptr)
nop
- ldftn int GetCallbacksType().CallbackNonStatic(int)
+ ldftn int typeof(Callbacks).CallbackNonStatic(int)
stloc.0
ldloc.0
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);
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);
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);
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);
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