samedi 3 janvier 2015

trap an event raised by a reflected dll

I have a desktop C# app.


I load a DLL dynamically.


I attach to an event.


I now want to trap the event in my app.


So, this is the DLL I am reflecting:



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


This is part of 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");
var flags = BindingFlags.Static | BindingFlags.Public;
MethodInfo Method = type.GetMethod("hello", flags);
var eventInfo = type.GetEvent("ResponseEvent", flags);
type.InvokeMember("hello", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, "", null);
}
catch (Exception ex)
{

}
}


I now need something like this:



private void FunctionInMyDEsktopApp(string message)
{
//this has been raised in my DLL
}





Aucun commentaire:

Enregistrer un commentaire