samedi 3 janvier 2015

I cannot subscribe to my event using reflection

I have a c# desktop app.


I load a DLL using reflection. The DLL is loaded as bytes.


I need to bind to the event in the DLL.


The eventInfo is null.


This is my code:


//in my DLL



namespace injectdll
{
public class Class1
{
public delegate void delResponseEvent(string message);
public static event delResponseEvent ResponseEvent;
public static void hello()
{
ResponseEvent("hello andy");
}
}
}


//in my desktop app



private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\Andrew\Desktop\testbytes\injectdll\injectdll\bin\Debug\injectdll.dll");
Assembly program = Assembly.Load(bytes);
Type type = program.GetType("injectdll.Class1");
MethodInfo Method = program.GetTypes()[0].GetMethod("hello");
type.InvokeMember("hello", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, "", null);
var eventInfo = program.GetType().GetEvent("ResponseEvent");

//eventinfo is null?
}
catch (Exception ex)
{

}
}





Aucun commentaire:

Enregistrer un commentaire