C # About reflection events

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.