zoukankan      html  css  js  c++  java
  • reflection_demo

    对于反射的理解:

    对于可以配置容易实现,配置对应的dll名称进去,然后系统中配置后即可调用执行,使得程序按照模块划分互不影响;

    在用的时候再进行绑定,如下定义了一个类,uu 中两个属性user_no,user_name,构造函数对于,一个方法,将生成的dll放在主程序的执行目录下来做调用;

    主程序:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using System.Reflection;


    namespace reflection_demo
    {
    class Program
    {
    static void Main(string[] args)
    {
    //反射得到对象的实例
    //1.通过获取具体的dll来进行得到
    string file_name = System.Reflection.Assembly.GetExecutingAssembly().Location;

    file_name = file_name.Remove(file_name.LastIndexOf("\") + 1) + "user.dll";

    System.Reflection.Assembly assmeply_array = System.Reflection.Assembly.LoadFile(file_name);

    foreach (var item in assmeply_array.GetTypes())
    {
    if (item.Name=="uu")
    {

    //得到一个类型的对象 对于构造函数需要有固定的参数
    var tt = Activator.CreateInstance(item, "aa", "dd");

    //得到一个方法的实例 ---对于方法名的统一可以集成同一个接口里面写好具体的方法名
    MethodInfo md_array = item.GetMethod("print_info");

    //得到属性
    //item.GetProperty();

    //
    //item.GetField();

    //执行对应的方法
    md_array.Invoke(tt,null);

    Console.ReadKey();

    }
    }

    }
    }
    }

    uu类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace user
    {
    class uu
    {
    public string user_no { get; set; }

    public string user_name { get; set; }

    public uu(string str_no, string str_name)
    {
    this.user_no = str_no;
    this.user_name = str_name;

    }


    public void print_info()
    {
    Console.WriteLine(this.user_no +"--->"+this.user_name);
    }

    }
    }

  • 相关阅读:
    软件测试课堂练习
    JSP第一次作业
    安卓第六次作业
    安卓第五次作业
    第四次安卓作业
    JSP第四周
    软件测试课堂练习3.4
    Android数据库表
    Android购物菜单
    Android增删改查
  • 原文地址:https://www.cnblogs.com/muzililong/p/10852749.html
Copyright © 2011-2022 走看看