c# 反射事件

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   for   檔案   div   art   

被反射類中:

  

  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;        }

 

c# 反射事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.