public class PlugingManager
    {
        //插件装载器
        public ArrayList Plugins = new ArrayList();
        //插件FullName
        public ArrayList PlugFullName = new ArrayList();
        //插件类型
        public ArrayList PlugTypes = new ArrayList();

#region 构造函数
        /// <summary>
        /// PlugingManager插件加载
        /// </summary>
        /// <param name="plugspath">插件所在目录必须是运行目录中的文件夹</param>
        /// <param name="StartsWith">加载指定插件(插件包含的名称)</param>
        /// <param name="InterfaceName">插件接口名称</param>
        public PlugingManager(string plugspath,string StartsWith,string InterfaceName)
        {
            //获取插件目录(plugins)下所有文件
            string[] files = Directory.GetFiles(Application.StartupPath + @"\\" + plugspath);
           
            foreach (string file in files)
            {
                if (file.ToUpper().EndsWith(StartsWith.ToUpper()))
                {
                    try
                    {
                        //Assembly ab = Assembly.LoadFrom(file);
                        Assembly ab = null;

//先将插件拷贝到内存缓冲
                        byte[] addinStream = null;
                        using(FileStream input = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            BinaryReader reader = new BinaryReader(input);
                            addinStream = reader.ReadBytes((int) input.Length);

}
                        ab = Assembly.Load(addinStream); //加载内存中的Dll
                        Type[] types = ab.GetTypes();
                        foreach (Type t in types)
                        {
                            if (t.GetInterface(InterfaceName) != null)
                            {
                                Plugins.Add(ab.CreateInstance(t.FullName));
                                PlugFullName.Add(t.FullName);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
        #endregion
    }

PlugingManager plug = new PlugingManager("Plugs", "Garden.Plugs.dll", "IPlug");

var win = plug.Plugins.ToArray().FirstOrDefault(m => ((Type)m.GetType()).Name.ToLower() == this.Tag.ToString().ToLower());
            MethodInfo OnShowDlg = ((Type)win.GetType()).GetMethod("ShowSelf");
            Form cl = (Form)OnShowDlg.Invoke(win, null);
            cl.WindowState = FormWindowState.Maximized;
            cl.MdiParent = this;
            cl.Show();
            foreach (object obj in plug.Plugins)
            {

Type t = obj.GetType();
                MethodInfo OnShowDlg = t.GetMethod("ShowSelf");
                Control cl = (Control)OnShowDlg.Invoke(obj, null);

Control con = GetControlFromForm(t.Name, this);
                if (con != null)
                {
                    con.Controls.Add(cl);
                    cl.Dock = DockStyle.Fill;
                    isbreak = false;
                    con = null;
                }

}

上面是从内存中生成对象

//获取程序集 
            Assembly assembly = Assembly.Load("环环环环环");
            Assembly assembly2 = Assembly.LoadFrom("环环环环环.dll");             //从程序集中获取指定对象类型;
            Type type = assembly.GetType("环环环环环.User");             //使用Activator创建实例(无参数构造函数)
            var user1 = Activator.CreateInstance(type);
            //使用Activator创建实例(带参数构造函数)
            var user2 = Activator.CreateInstance(type, "薄暮");             //使用Assembly创建实例(无参数构造函数)
            var user3 = assembly.CreateInstance("环环环环环.User");             //反射无参构造函数
            ConstructorInfo constructor1 = type.GetConstructor(new Type[] {});
            var user4 = constructor1.Invoke(new object[] { });             //反射有参构造函数 
            ConstructorInfo constructor2 = type.GetConstructor(new Type[] { typeof(string) });
            var user5 = constructor2.Invoke(new object[] { "薄暮" });             //调用public函数(无参数)
            type.InvokeMember("PublicShow",
                              BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, user1,
                              null);             //调用public函数(带参数)
            string returnValue =
                type.InvokeMember("GetString",
                                  BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, user1,
                                  new object[] { "薄暮" }) as string;             // 调用静态方法       
            string returnValue2 =
                type.InvokeMember("StaticMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                                  null, null, new object[] {}) as string;             // 调用私有方法 .       
            type.InvokeMember("PrivateShow",
                              BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, user1,
                              new object[] {});             //反射属性
            var Name =
                type.InvokeMember("Name", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null,
                                  user1, new object[] {}) as string;             //设置属性(设置Name属性为"新属性")
            type.InvokeMember("Name", BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance, null,
                                  user1, new object[] {"新属性"});             //反射字段
            string Field =
                type.InvokeMember("Field", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance, null,
                                  user1, new object[] {}) as string;             //设置字段(设置Field字段为"新字段")
            type.InvokeMember("Field", BindingFlags.SetField | BindingFlags.Public | BindingFlags.Instance, null,
                                  user1, new object[] { "新字段" });

这是标准的用法,还挺好玩的吧

C# 反射相关的东西的更多相关文章

  1. DSAPI多功能组件编程应用-反射相关

    [DSAPI.DLL下载地址]       在.Net中,反射技术是一种入门困难,熟用快速的东西,对于没有接触过反射技术的程序员来说的确是头疼的,看一旦自己写过了,上手就非常简单了.在本节,将部分.N ...

  2. Java Class与反射相关的一些工具类

    package com.opslab.util; import org.apache.log4j.Logger; import java.io.File;import java.io.IOExcept ...

  3. 乐字节Java反射之四:反射相关操作

    大家好,乐字节小乐继续为Java初学者讲述Java基础知识.上次说到乐字节Java反射之三:方法.数组.类加载器,这次是Java反射之四:反射相关操作 1.操作属性 //1.获取Class对象 Cla ...

  4. java反射相关

    反射的机制:反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言 ...

  5. [C#]反射相关

    //Type type = DataHelper.Instance.GetType(); //MethodInfo[] infos = type.GetMethods(BindingFlags.Ins ...

  6. .Net实战之反射相关类之间的人体经络关系

    --1.类的部分组成成员 --2.巧记成员之间的关系 [MyTable("T_UserInfo")] public class UserInfo : Person, UserSer ...

  7. Cesium中常用的一些地理数据文件 以及数据相关的东西

    KML Cesium.KmlDataSource.load CZML Cesium.CzmlDataSource.load GeoJson Cesium.GeoJsonDataSource.load ...

  8. android一些系统相关的东西

    添加快捷方式和删除快捷方式: private void addShortcut() { Intent shortcut = new Intent( "com.android.launcher ...

  9. 编译用到boost相关的东西,问题的解决;以及和googletest库

    编译https://github.com/RAttab/reflect, 发现需要gcc4.7以上的版本才行.于是编译安装最新的gcc-6.2.0, 过程算顺利. http://www.linuxfr ...

随机推荐

  1. laravel创建定时任务

    官方文档给出的教程已经很详细了,这里给出一些补充帮助大家理解. 英文文档:https://laravel.com/docs/5.2/scheduling 中文文档:https://laravel-ch ...

  2. date格式化

    Linux: [ghsea@localhost ~]date +%Y:%m:%d [ghsea@localhost ~]date +%Y-%m%d [ghsea@localhost ~]date +% ...

  3. QTP脚本汇总比较有价值

    1.Object Spy的Tips Hold the CTRL key to change the window focus or perform other mouse operations  2. ...

  4. ios 点击放大图片,保存至手机相册

    直接贴.m文件代码 #import "UIImageView+Scale.h" static CGRect oldframe; @implementation UIImageVie ...

  5. 修改虚拟机内容导致oracle不能启动

    虚拟机内存目前设置为4G,想要改变成2G,数据库启动时导致报targetmomory错误,解决办法如下: 1.查看分配的memory_target和memory_max_target大小 SQL> ...

  6. ssh无密码登录设置方法以及出现问题 ECDSA host key 和IP地址对应的key不同的解决

    最近在做hadoop,因为要求各主机之间的用户必须相同,且为方便远程登录,需配置无密码登录 先附上ssh无密码登录设置方法: 先生成密钥并配置无ssh无密码登录本机,输入命令: ssh-keygen ...

  7. KMP算法的java实现

    package com.trs.utils; public class KMPStr { /* * 在KMP算法中,最难求的就是next函数,如何理解next函数是一个难题,特别是k=next[k], ...

  8. iOS开发设置关于tabBar和navigationBar以及item中的一些全局属性

    /* To set item label text attributes use the appearance selectors available on the superclass, UIBar ...

  9. boostrap插件

    第一章:模态弹出框 一.导入JavaScript插件 Bootstrap的JavaScript插件可以单独导入到页面中,也可以一次性导入到页面中.因为在Bootstrap中的JavaScript插件都 ...

  10. Ubuntu shortcuts

    Ubuntu shortcuts 打开系统管理器 gnome-system-monitor