C # reflection events

Source: Internet
Author: User

In the reflected class:

  public delegate void CompeletedHandler();        public static event CompeletedHandler AnalysisCompeleted; static void BasePath_AnalysisCompeleted()        {            if (AnalysisCompeleted != null)                AnalysisCompeleted();        }

 

 

Class used for reflection:

namespace notam{     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }           public static void SS()         {             //return null;         }     }}

 

 

        public static object InvokeClassMethod(string className,string methodName,object[] parameters,Type type)        {            object objClass =Cores.ClassBuilder.CreateObject(className);            Type tt = objClass.GetType();            System.Reflection.MethodInfo mi = tt.GetMethod(methodName);            System.Reflection.EventInfo ee = tt.GetEvent("AnalysisCompeleted");            ee.AddEventHandler(objClass, Delegate.CreateDelegate(ee.EventHandlerType,type, "SS"));            parameters = new object[] { parameters };            return mi.Invoke(objClass, parameters);        }

 

 

/// <Summary> /// create an object based on the class name. /// </Summary> /// <Param name = "classname"> </param> /// <returns> </returns> Public static object Createobject (string classname) {object instance = NULL; string [] S = classname. split (','); If (S. length> = 2) {string filename = s [1]; If (filename. indexof (':') <0) // The path {If (appdomain. currentdomain. setupinformation. applicationbase. endswith (@ "\") filename = appdomain. currentdomain. setupstreams Ion. applicationbase + s [1]; else filename = appdomain. currentdomain. setupinformation. applicationbase + @ "\" + s [1];} If (file. exists (filename) {Assembly ASM = assembly. loadfrom (filename); instance = ASM. createinstance (s [0]);} else {Throw new invaliddataexception ("The fbfactory configuration is incorrect in the configuration file and the dynamic library file cannot be found:" + filename + "! ") ;}} Else {// instance = assembly. getexecutingassembly (). createinstance (classname); instance = assembly. loadfrom (classname ). createinstance ("finemap. finemapmain "); If (instance = NULL) {instance = assembly. getcallingassembly (). createinstance (classname);} If (instance = NULL) {ienumerator Ie = appdomain. currentdomain. getassemblies (). getenumerator (); While (ie. movenext () {Assembly AB = IE. cu Rrent as assembly; If (AB! = NULL) {instance = AB. createinstance (classname); If (instance! = NULL) Break ;}}} return instance ;}

 

C # reflection events

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.