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("加载插件失败!请验证插件的正确性...", "错误");
                }
            }
  • 相关阅读:
    File
    Include and Require
    Date and Time
    css3的nth-child选择器使用示例
    document.title
    php获取从百度搜索进入网站的关键词的详细代码
    Iphone手机、安卓手机浏览器控制默认缩放大小的方法
    离线宝调用
    织梦DedeCMS网站地图模板
    禁止选择文本和禁用右键 v3.0
  • 原文地址:https://www.cnblogs.com/gghxh/p/1223168.html
Copyright © 2011-2022 走看看