首页 > 代码库 > c# 反射事件
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; }
c# 反射事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。