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("加载插件失败!请验证插件的正确性...", "错误");
                }
            }
  • 相关阅读:
    Vim编辑器-Basic Visual Mode
    Vim编辑器-Windows
    Vim编辑器-Searching
    Vim编辑器-Text Blocks and Multiple Files
    Vim编辑器-Editing a Little Faster
    Vim编辑器-Basic Editing
    Android12系统源码分析:NativeTombstoneManager
    为什么色彩管理很重要?
    使用chrome调试代码时引入jquery
    抖音、微信超火中国红头像制作
  • 原文地址:https://www.cnblogs.com/gghxh/p/1223168.html
Copyright © 2011-2022 走看看