zoukankan      html  css  js  c++  java
  • 反射 基于反射的简单工厂

    反射 访问修饰符只是给编译器看的,反射中可以完全操控到无论私有不私有
    (GAC :Gobal Assemble Cache)全局程序集缓存
    得到assembly的方法
    1.0*在当前运行目录下面 加载对应的程序集
    assembly ass1=assembly.load(“lib”)
    2.0加载制定目录下面的程序集
    assembly.loadfile()或者loadfrom()
    3.0加载当前运行程序域中的所有程序集
    Assembly[] asss = AppDomain.CurrentDomain.GetAssemblies();
    (GAC :Gobal Assemble Cache)全局程序集缓存
    4.0通过当前对象获得当前运行的程序集
    this.getType().assembly;
    5.0通过type得到运行的程序集
    type t=typeof(...);
    t.assembly;

    得到type的几种方法
    1.0通过typeof方法得到type
    Type t1 = typeof(Form1);
    2.0根据对象得到type
    Person p = new Person();
    Type t2 = p.GetType();
    3.0根据程序集得到对应的type
    Assembly ass = Assembly.Load("Lib");
    Type t3 = ass.GetType("Lib.GrilFriend");
    4.0得到当前程序集的所有Type
    Type[] t4s = ass.GetTypes();
    5.0得到当前程序集中所有公共的type
    Type[] t5s = ass.GetExportedTypes();



    反射创建对象两种方式

    1.0这两种方式创建对象,类中必须要有无参数的构造函数
    //0.0得到运行的程序集
    Assembly ass = Assembly.Load("Lib");
    //1.0通过程序集来创建
    object obj1 = ass.CreateInstance("Lib.GrilFriend");
    //2.0通过Activator来创建对象
    //得到类对应的Type
    Type type = ass.GetType("Lib.GrilFriend");
    object obj2 = Activator.CreateInstance(type);
    2.0如何创建类中没有无参的构造函数的类的对象
    //0.0得到运行的程序集
    Assembly ass = Assembly.Load("Lib");
    //得到类对应的Type
    Type type = ass.GetType("Lib.GrilFriend");
    //得到当前类中的构造函数
    ConstructorInfo cinfo = type.GetConstructor(new Type[] { typeof(string), typeof(int) });
    //执行构造函数
    object obj3 = cinfo.Invoke(new object[] { "刘亦菲", 28 });

    使用反射实现记事本热插拔插件。一个主记事本程序,我来规定接口,别人开发

    插件,摆放到规定的文件夹位置
    利用反射调用其中的方法来实现 大小写切换功能。
    接口规定了该插件功能名称和插件功能。。。。一个只读属性一个大小写转换方法

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace 记事本
    11 {
    12     using System.Reflection;
    13     public partial class Form1 : Form
    14     {
    15         public Form1()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         private void Form1_Load(object sender, EventArgs e)
    21         {
    22             //定位到存放插件的位置
    23             string basePath = AppDomain.CurrentDomain.BaseDirectory;
    24             string phyPath = basePath + "Iplugs//";
    25             //得到所有插件位置字符串遍历
    26             string[] strs = System.IO.Directory.GetFiles(phyPath, "*.dll");
    27             Type itype = typeof(IPlugs.Iplugs01);
    28             if (strs.Length > 0)
    29             {
    30                 foreach (string item in strs)
    31                 {
    32                     //得到程序集
    33                     Assembly ass = Assembly.LoadFile(item);
    34                     //得到程序集中类数组遍历
    35                     Type[] types = ass.GetTypes();
    36                     if (types.Length > 0)
    37                     {
    38                         foreach (var type in types)
    39                         {
    40                             //判断实现了接口
    41                             if (itype.IsAssignableFrom(type))
    42                             { //根据type创建对象出来
    43                                 object obj = Activator.CreateInstance(type);
    44                                 //得到属性
    45                                 PropertyInfo proinfo = type.GetProperty("Name");
    46                                 //取出属性值
    47                                 object proobj = proinfo.GetValue(obj);
    48                                 //给菜单来一个menuitem并且绑定一个点击事件
    49                                 ToolStripMenuItem stmi = new ToolStripMenuItem(proobj.ToString());
    50                                 stmi.Tag = type;
    51                                 stmi.Click += stmi_Click;
    52                                 this.ms1.Items.Add(stmi);
    53                             }
    54                         }
    55                     }
    56                 }
    57             }
    58         }
    59 
    60         void stmi_Click(object sender, EventArgs e)
    61         {
    62             string txt = this.textBox1.Text;
    63             ToolStripMenuItem stmi = sender as ToolStripMenuItem;
    64             Type type = stmi.Tag as Type;
    65             //得到方法
    66             MethodInfo methinfo = type.GetMethod("Process", new Type[] { typeof(string) });
    67             //创建对象
    68             object obj = Activator.CreateInstance(type);
    69             //执行方法
    70             object str = methinfo.Invoke(obj, new object[] { txt });
    71             //返回值赋回
    72             textBox1.Text = str.ToString();
    73 
    74         }
    75     }
    76 }
    反射实现热插拔插件
  • 相关阅读:
    匹配域名
    异步加载js文件
    Python3.X BeautifulSoup([your markup], "lxml") markup_type=markup_type))的解决方案
    CSDNmarkdown编辑器直接写代码的小效果(一生愿)
    JAVA_OA(十四)番外:JAVAWEB防止表单重复提交的方法整合(包括集群部署)
    JAVA_OA(十四):SSM练手项目bug-Oracle分页web页面无法转到下一页
    JAVA_OA(十四):SSM练手项目bug-JSP页面传递参数的编码问题
    JAVA_OA(八):springMVC对JDBC的操作小项目b
    完全卸载oracle11g教程、Oracle11g的卸载方法和步骤
    JAVA_OA(八):springMVC对JDBC的操作小项目a
  • 原文地址:https://www.cnblogs.com/zhanying/p/4154262.html
Copyright © 2011-2022 走看看