zoukankan      html  css  js  c++  java
  • 利用反射调用类及其属性和方法


            /// <summary> 从DLL获取对象
            /// </summary>
            /// <param name="PControl"></param>
            /// <param name="dllPath"></param>
            /// <param name="NameSpacePath"></param>
            /// <param name="className"></param>
            public object LoadObjectByDll(string dllPath, string NameSpacePath, string className,bool isExePreLoad)
            {
                try
                {
                    //加载控件        
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(dllPath);
                    //获得类(型)        
                    Type type = assembly.GetType(String.Format("{0}.{1}", NameSpacePath, className), false, true);
                    //设置筛选标志        
                    System.Reflection.BindingFlags bflags = System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                    //调用构造函数并获得对象        
                    Object obj = type.InvokeMember(className, bflags | System.Reflection.BindingFlags.CreateInstance, null, null, null);
                    //if (isExePreLoad)
                    //{
                    //    object obj2;
                    //    PropertyInfo info = type.GetProperty("isPreLoad");
                    //    obj2 = info.GetValue(obj,null);
                    //    //obj2 = type.InvokeMember("isPreLoad", BindingFlags.GetProperty | BindingFlags.Public, null, (IPlugin)obj, null);
                    //    if (obj2.ToString().ToLower() == "true")
                    //    {
                    //        MethodInfo mi = type.GetMethod("PreLoad");
                    //        mi.Invoke(obj, null);
                    //    }
                    //}
                    return obj;
                }
                catch (Exception ex)
                {
                    throw ex;
                    //MessageBox.Show("加载插件失败!请验证插件的正确性...", "错误");
                }
            }
  • 相关阅读:
    Python中的sorted函数以及operator.itemgetter函数
    a=a+(a++);b=b+(++b);计算顺序,反汇编
    带基虚类的构造函数执行顺序
    开源系统管理资源大合辑
    linux的LNMP架构介绍、MySQL安装、PHP安装
    lamp下mysql安全加固
    ITSS相关的名词解释
    从苦逼到牛逼,详解Linux运维工程师的打怪升级之路
    Linux 文件系统概览
    Exchange2010批量删除邮件
  • 原文地址:https://www.cnblogs.com/gghxh/p/1223168.html
Copyright © 2011-2022 走看看