using System; using System.Runtime.InteropServices; delegate void MyDelegate(int value); class Program { static void Main(string[] args) { MyDelegate myDelegate = new MyDelegate(DoSomething); IntPtr ptr = Marshal.GetFunctionPointerForDelegate(myDelegate); // pass ptr to unmanaged code as a function pointer } static void DoSomething(int value) { Console.WriteLine("Doing something with value {0}", value); } }
using System; using System.Runtime.InteropServices; enum MyEnum { Value1, Value2 } delegate MyEnum MyDelegate(int value); class Program { static void Main(string[] args) { MyDelegate myDelegate = new MyDelegate(DoSomething); IntPtr ptr = Marshal.GetFunctionPointerForDelegate(myDelegate); // pass ptr to unmanaged code as a function pointer } static MyEnum DoSomething(int value) { Console.WriteLine("Doing something with value {0}", value); return MyEnum.Value1; } }This code creates a delegate object that returns an enum value, and gets its function pointer using `Marshal.GetFunctionPointerForDelegate`. The pointer can then be passed to unmanaged code as a function pointer. Package library: This method is part of the .NET Framework and is included in the `System.Runtime.InteropServices` namespace, which is part of the `mscorlib.dll` assembly.