c# 反射事件

被反射类中:

  

  public delegate void CompeletedHandler();


        public static event CompeletedHandler AnalysisCompeleted;


 static void BasePath_AnalysisCompeleted()
        {
            if (AnalysisCompeleted != null)
                AnalysisCompeleted();
        }

反射时用的类中:

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>
        /// 根据类名创建对象。
        /// </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)   //不包括路径
                {
                    if (AppDomain.CurrentDomain.SetupInformation.ApplicationBase.EndsWith(@""))
                        filename = AppDomain.CurrentDomain.SetupInformation.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("配置文件中,FBFactory配置不正确,找不到动态库文件:" + 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.Current as Assembly;
                        if (ab != null)
                        {
                            instance = ab.CreateInstance(className);
                            if (instance != null)
                                break;
                        }
                    }
                }
            }

            return instance;
        }
原文地址:https://www.cnblogs.com/weihongli/p/3976748.html