码迷,mamicode.com
首页 > 其他好文 > 详细

c# 反射事件

时间:2014-09-17 13:24:12      阅读:179      评论:0      收藏:0      [点我收藏+]

标签: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# 反射事件

标签:style   blog   color   io   ar   for   文件   div   art   

原文地址:http://www.cnblogs.com/weihongli/p/3976748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!