Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Reflection;
- public interface IMyInterface
- {
- void MyFunction();
- }
- public struct MyStruct : IMyInterface
- {
- public void MyFunction() => Console.WriteLine("Hello World!");
- }
- public unsafe class Program
- {
- const int TestMode = 1;
- public static void Main()
- {
- MyStruct instance = default;
- MyStruct* ptr = &instance;
- MethodInfo method = typeof(MyStruct).GetMethod("MyFunction");
- delegate*<void*, void> func1 = (delegate*<void*, void>)method.MethodHandle.GetFunctionPointer();
- switch (TestMode)
- {
- case 1:
- func1(ptr); //System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.'
- break;
- case 2:
- func1((void*)1); //System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.'
- break;
- case 3:
- func1((void*)0); //no error
- func1(ptr); //no error
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement