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!
Get the event with
typeof(B).GetEvent("Test")and then hook it up withEventInfo.AddEventHandler. Sample code: