使用反射将委托连接到事件?

发布于 2024-10-22 21:39:29 字数 917 浏览 3 评论 0原文

我有 2 个 DLL,A.dll 包含:

namespace Alphabet
{
    public delegate void TestHandler();

    public class A  
    {
        private void DoTest()
        {
            Type type = Assembly.LoadFile("B.dll").GetType("Alphabet.B");                       

            Object o = Activator.CreateInstance(type);

            string ret = type.InvokeMember("Hello", BindingFlags.InvokeMethod | BindingFlags.Default, null, o, null);
        }
    }
}

B.dll 包含

namespace Alphabet
{   
    public class B
    {
        public event TestHandler Test();

        public string Hello()
        {
            if (null != Test) Test();
            return "Hello";
        }
    }
}

我正在使用 InvokeMember 从 B.dll 获取结果,我还希望 B.dll Test()< /code> 返回结果之前。那么,如何通过反射将delegate挂接到B.dll中的event呢?

任何帮助将不胜感激!

I have 2 DLL, A.dll contains:

namespace Alphabet
{
    public delegate void TestHandler();

    public class A  
    {
        private void DoTest()
        {
            Type type = Assembly.LoadFile("B.dll").GetType("Alphabet.B");                       

            Object o = Activator.CreateInstance(type);

            string ret = type.InvokeMember("Hello", BindingFlags.InvokeMethod | BindingFlags.Default, null, o, null);
        }
    }
}

and B.dll contains

namespace Alphabet
{   
    public class B
    {
        public event TestHandler Test();

        public string Hello()
        {
            if (null != Test) Test();
            return "Hello";
        }
    }
}

I'm using InvokeMember to get the result from B.dll and I also want B.dll to Test() before return the result. So, how can I hook up the delegate to the event in B.dll through reflection?

Any helps would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-10-29 21:39:29

使用 typeof(B).GetEvent("Test") 获取事件,然后使用 EventInfo.AddEventHandler 将其挂钩。示例代码:

using System;
using System.Reflection;

public delegate void TestHandler();

public class A  
{
    static void Main()
    {
        // This test does everything in the same assembly just
        // for simplicity
        Type type = typeof(B);
        Object o = Activator.CreateInstance(type);

        TestHandler handler = Foo;
        type.GetEvent("Test").AddEventHandler(o, handler);
        type.InvokeMember("Hello",
                          BindingFlags.Instance | 
                          BindingFlags.Public |
                          BindingFlags.InvokeMethod,
                          null, o, null);
    }

    private static void Foo()
    {
        Console.WriteLine("In Foo!");
    }
}

public class B
{
    public event TestHandler Test;

    public string Hello()
    {
        TestHandler handler = Test;
        if (handler != null)
        {
            handler();
        }
        return "Hello";
    }
}

Get the event with typeof(B).GetEvent("Test") and then hook it up with EventInfo.AddEventHandler. Sample code:

using System;
using System.Reflection;

public delegate void TestHandler();

public class A  
{
    static void Main()
    {
        // This test does everything in the same assembly just
        // for simplicity
        Type type = typeof(B);
        Object o = Activator.CreateInstance(type);

        TestHandler handler = Foo;
        type.GetEvent("Test").AddEventHandler(o, handler);
        type.InvokeMember("Hello",
                          BindingFlags.Instance | 
                          BindingFlags.Public |
                          BindingFlags.InvokeMethod,
                          null, o, null);
    }

    private static void Foo()
    {
        Console.WriteLine("In Foo!");
    }
}

public class B
{
    public event TestHandler Test;

    public string Hello()
    {
        TestHandler handler = Test;
        if (handler != null)
        {
            handler();
        }
        return "Hello";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文