Code in the Frmmain class
private void Startrun (string tag, string date, bool TipType)
{
var dllpath = string. Format ("{0}.{ 1} ", GetType (). Namespace, tag);
var assembly = Assembly.Load (DllPath);
var Classfullpath = string. Format ("{0}.{ 1}. {2} ", GetType (). Namespace, Tag, "RUNBTW");
var type = assembly. GetType (Classfullpath);
object instance = assembly. CreateInstance (Classfullpath);
var eventInfo = type. GetEvent ("Appendtips", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
var tdelegate = Eventinfo.eventhandlertype;
var mihandler = typeof (Frmmain). GetMethod ("Appendtips", BindingFlags.Public | BindingFlags.Static);
var d = delegate.createdelegate (TDelegate, Mihandler);
var miaddhandler = Eventinfo.getaddmethod ();
Object[] Addhandlerargs = {d};
Miaddhandler.invoke (instance, Addhandlerargs);
var method = type. GetMethod ("Startrun");
Method. Invoke (instance, new object[] {date, tiptype});
}
public static void Appendtips (string tips, color color, bool tipstype)
{
if (frm.richResultAuto.InvokeRequired)
{
Appendtipsdelegate ATD = new Appendtipsdelegate (Tips);
frm. Invoke (ATD, new object[] {tips, color, tipstype});
}
Else
{
Tips (tips, color, tipstype);
}
}
Dynamically loading DLL files Assembly.Load loading a relative path and assembly.loadfile requires an absolute path
The Appendtips method in the Frmmain class must be public static or not bind.
var eventInfo = type. GetEvent ("Appendtips", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | bindingflags.static); Appendtips here is the name of the Appendtips event in the RUNBTW class that is reflected.
public class RUNBTW
{
public delegate void Appendtipsdelegate (string tips, color color, bool tipstype);
public event Appendtipsdelegate Appendtips;
public void Startrun (string date, bool Tipstype)
{
}
}
The Startrun method in RUNBTW is that there is no return value if there is a return value, such as returning an int type
Var cusrerurnvalue= method. Invoke (instance, new object[] {date, tiptype});
The cusrerurnvalue here is the return value.
Reference from: http://bbs.csdn.net/topics/330067707
C # About reflection events