我可以组合2个不同的代表 [英] Can I Combine 2 Different Delegates

查看:52
本文介绍了我可以组合2个不同的代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
    }
}





然后



Foo.Method(); //执行方法并在控制台中写入



重点是包装T代理并为其添加功能。



可以这样做吗?



then

Foo.Method(); // executes the method and writes in console

The point is to wrap the T delegate and add functionality to it.

Can this be done ?

推荐答案

delegate void EnclosingDelegate(string parameter);

delegate string EnclosedDelegate();

class foo
{
   void main() {
      EnclosedDelegate enclosed = () => "foo";
      EnclosingDelegate enclosing = (s) => s = enclosed();
   }
}



这就是您要搜索的内容吗?


Is that what you are searching for?


这篇关于我可以组合2个不同的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆