使用反射将委托连接到事件?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
typeof(B).GetEvent("Test")
获取事件,然后使用EventInfo.AddEventHandler
将其挂钩。示例代码:Get the event with
typeof(B).GetEvent("Test")
and then hook it up withEventInfo.AddEventHandler
. Sample code: