zoukankan      html  css  js  c++  java
  • .NET和JAVA 反射对比

    反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息。使用反射可以看到一个程序集内部的接口、类、方法、字段、属性、特性等等信息。在System.Reflection命名空间内包含多个反射常用的类,下面表格列出了常用的几个类。

    .NET 版

    类型 作用
    Assembly 通过此类可以加载操纵一个程序集,并获取程序集内部信息
    EventInfo 该类保存给定的事件信息
    FieldInfo 该类保存给定的字段信息
    MethodInfo 该类保存给定的方法信息
    MemberInfo 该类是一个基类,它定义了EventInfo、FieldInfo、MethodInfo、PropertyInfo的多个公用行为
    Module 该类可以使你能访问多个程序集中的给定模块
    ParameterInfo 该类保存给定的参数信息      
    PropertyInfo 该类保存给定的属性信息

    一、System.Reflection.Assembly类

         通过Assembly可以动态加载程序集,并查看程序集的内部信息,其中最常用的就是Load()这个方法。

         Assembly assembly=Assembly.Load("MyAssembly");

    二、System.Type类

         Type是最常用到的类,通过Type可以得到一个类的内部信息,也可以通过它反射创建一个对象。一般有三个常用的方法可得到Type对象。

    1. 利用typeof() 得到Type对象

      Type type=typeof(Example);

    2. 利用System.Object.GetType() 得到Type对象

      Example example=new Example();

      Type type=example.GetType();

    3. 利用System.Type.GetType() 得到Type对象

      Type type=Type.GetType("MyAssembly.Example",false,true);

      注意参数0是类名,参数1表示若找不到对应类时是否抛出异常,参数1表示类名是否区分大小写

       例子:

       我们最常见的是利用反射与Activator结合来创建对象。

       Assembly assembly= Assembly.Load("MyAssembly");

       Type type=assembly.GetType("Example");

       object obj=Activator.CreateInstance(type);

    三、反射方法

        1.通过 System.Reflection.MethodInfo能查找到类里面的方法

        代码:

        Type type=typeof(Example);

        MethodInfo[] listMethodInfo=type.GetMethods();

        foreach(MethodInfo methodInfo in listMethodInfo)

             Cosole.WriteLine("Method name is "+methodInfo.Name);

      

       2.我们也能通过反射方法执行类里面的方法

       代码(经常用到):

       Assembly assembly= Assembly.Load("MyAssembly");

       Type type=assembly.GetType("Example");

       object obj=Activator.CreateInstance(type);

       MethodInfo methodInfo=type.GetMethod("Hello World");  //根据方法名获取MethodInfo对象

       methodInfo.Invoke(obj,null);  //参数1类型为object[],代表Hello World方法的对应参数,输入值为null代表没有参数

    更多详细点击这里

    下面代码是获取程序集的

     1 System.Reflection.Assembly ass = Assembly.LoadFrom(Server.MapPath("bin/Reflection.dll")); //加载DLL
     2 foreach (Type temp in alltype)
     3 {
     4     Console.WriteLine(temp.Name);  //打印出MyClass程序集中的所有类型名称 MyClass , Demo
     5 }
     6 System.Type t = ass.GetType("Reflection.Test");//获得类型
     7 object o = System.Activator.CreateInstance(t);//创建实例
     8 
     9 System.Reflection.MethodInfo mi = t.GetMethod("Hello Word");//获得方法
    10 
    11 mi.Invoke(o, null)"});//调用方法

    参考:

    http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html

    http://www.cnblogs.com/wangshenhe/p/3256657.html

    JAVA 版

  • 相关阅读:
    疫情环境下的网络学习笔记 python 5.8 数据库入门终章
    疫情环境下的网络学习笔记 python 5.7 navicat数据库,例题,sql注入
    疫情环境下的网络学习笔记 python 5.6 暂时看看
    疫情环境下的网络学习笔记 python 5.5 MYSql 表关系,外键
    疫情环境下的网络学习笔记 python 5.4 数据库基础
    疫情环境下的网络学习笔记 python 4.30 初识数据库
    疫情环境下的网络学习笔记 python 4.29 网络小项目
    XJOI 夏令营501-511测试11 游戏
    XJOI 夏令营501-511测试11 统计方案
    CF1197D Yet Another Subarray Problem
  • 原文地址:https://www.cnblogs.com/bigbrid/p/6930381.html
Copyright © 2011-2022 走看看