Advertisement
whitelava3203

what

Mar 23rd, 2025
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | Source Code | 0 0
  1. using System.Reflection;
  2.  
  3. public interface IMyInterface
  4. {
  5.     void MyFunction();
  6. }
  7.  
  8. public struct MyStruct : IMyInterface
  9. {
  10.     public void MyFunction() => Console.WriteLine("Hello World!");
  11. }
  12.  
  13. public unsafe class Program
  14. {
  15.     const int TestMode = 1;
  16.     public static void Main()
  17.     {
  18.         MyStruct instance = default;
  19.         MyStruct* ptr = &instance;
  20.         MethodInfo method = typeof(MyStruct).GetMethod("MyFunction");
  21.         delegate*<void*, void> func1 = (delegate*<void*, void>)method.MethodHandle.GetFunctionPointer();
  22.  
  23.         switch (TestMode)
  24.         {
  25.             case 1:
  26.                 func1(ptr); //System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.'
  27.                 break;
  28.             case 2:
  29.                 func1((void*)1); //System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.'
  30.                 break;
  31.             case 3:
  32.                 func1((void*)0); //no error
  33.                 func1(ptr); //no error
  34.                 break;
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement