class Foo { [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")] private static extern IntPtr GetProcAddress(IntPtr hModule, string procName); public delegate void TestMethod(uint cap); public static TestMethod Method; static Foo() { Method = Func<TestMethod>("TestMethod"); } public static T Func<T>(string name) where T : class { IntPtr _dll = IntPtr.Zero;// ptr to dll IntPtr ptr = GetProcAddress(_dll, name); Delegate a = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)); //Create delegate of type T but with different body of a => { Console.WriteLine("test"); }; Delegate b; //Multicast delegate return Delegate.Combine(a, b) as T; } }
delegate void EnclosingDelegate(string parameter); delegate string EnclosedDelegate(); class foo { void main() { EnclosedDelegate enclosed = () => "foo"; EnclosingDelegate enclosing = (s) => s = enclosed(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)