zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    phpstorm+xdebug配置
    php5.4 traits
    psr-4
    oAuth 认证和授权原理
    跨域解决方案
    【微信公众平台开发】利用百度接口,制作一键导航功能
    php 加密压缩
    jquery validate使用笔记
    where和having
    在join中,on和where的区别
  • 原文地址:https://www.cnblogs.com/weihongli/p/3976748.html
Copyright © 2011-2022 走看看