Java中的程序集加载和反射机制是什么?

您所在的位置:网站首页 il是什么软件 Java中的程序集加载和反射机制是什么?

Java中的程序集加载和反射机制是什么?

2023-05-18 01:21| 来源: 网络整理| 查看: 265

一、程序集的加载

JIT编译器器将IL代码编译成本地代码时, 会查看IL代码中引用了哪些类型。在运行过程中,JIT编译器利用程序集的TypeRef和AssemblyRef元数据表来确定哪一个程序集定义了所引用的类型,然后JIT编译器将对应程序集加载到AppDomain中,在内部,CLR使用System.Reflection.Assembly类的静态方法Load来尝试加载一个程序集。然而如果我们想动态加载一个程序集时,可以使用Assembly的Load方法来动态加载程序集,其中Assembly类中还提供了其他的加载程序集方法,有LoadFrom(string path), LoadFile(stringassemblyFile)等。

二、反射机制

.net中反射在运行中过程中解析程序集中的元数据,获得类型中的成员(包括字段、构造器、方法、属性、事件等)信息。

动态加载一个程序集并获得类型中的成员

把下面的类放在一个类库工程中,并编译生成程序集(例如为ClassLibrary1.dll,假设把dll放在D盘根目录下面)

public class ReflectTestClass { public string name; public int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } /// /// No Paramter Constructor /// public ReflectTestClass() { } /// /// Constructor with Parameter /// /// /// public ReflectTestClass(string names,int ages) { this.name = names; this.age = ages; } public string writeString(string name) { return "Welcome " + name; } public static string WriteName(string name) { return "Welcome "+name +" Come here"; } public string WirteNopara() { return "The method is no parameter "; } }登录后复制

然后建立一个控制台程序用来动态加载上面生成的程序集和输出类型中的成员,代码中有详细的介绍。

class Program { static void Main(string[] args) { Assembly ass; Type[] types; Type typeA; object obj; try { // 从本地中 加载程序集 然后从程序集中通过反射获得类型的信息的,并且调用方法 ass = Assembly.LoadFrom(@"D:\ClassLibrary1.dll"); types = ass.GetTypes(); foreach (Type type in types) { Console.WriteLine("Class Name is " + type.FullName); Console.WriteLine("Constructor Information"); Console.WriteLine("-----------------------"); // 获取类型的结构信息 ConstructorInfo[] myconstructors = type.GetConstructors(); ShowMessage(myconstructors); Console.WriteLine("Fields Information"); Console.WriteLine("-----------------------"); // 获取类型的字段信息 FieldInfo[] myfields = type.GetFields(); ShowMessage(myfields); Console.WriteLine("All Methods Information"); Console.WriteLine("-----------------------"); // 获取方法信息 MethodInfo[] myMethodInfo = type.GetMethods(); ShowMessage(myMethodInfo); Console.WriteLine("All Properties Information"); Console.WriteLine("-----------------------"); // 获取属性信息 PropertyInfo[] myproperties = type.GetProperties(); ShowMessage(myproperties); } // 用命名空间+类名获取类型 typeA = ass.GetType("ClassLibrary1.ReflectTestClass"); // 获得方法名称 MethodInfo method = typeA.GetMethod("writeString"); // 创建实例 obj = ass.CreateInstance("ClassLibrary1.ReflectTestClass"); string result = (String)method.Invoke(obj,new string[] {"Tom"}); Console.WriteLine("Invoke Method With Parameter"); Console.WriteLine("-----------------------"); Console.WriteLine(result); Console.WriteLine("-----------------------"); Console.WriteLine(); method = typeA.GetMethod("WriteName"); result = (string)method.Invoke(null,new string[] {"Tom"}); Console.WriteLine("Invoke Static Method with Parameter"); Console.WriteLine("-----------------------"); Console.WriteLine(result); Console.WriteLine("-----------------------"); Console.WriteLine(); method = typeA.GetMethod("WirteNopara"); Console.WriteLine("Invoke Method with NOParameter"); result = (string)method.Invoke(obj, null); Console.WriteLine("-----------------------"); Console.WriteLine(result); Console.WriteLine("-----------------------"); } catch(FileNotFoundException ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } /// /// 显示数组信息 /// /// /// public static void ShowMessage(T[] array) { foreach(T member in array) { Console.WriteLine(member.ToString()); } Console.WriteLine("-----------------------"); Console.WriteLine(); } }登录后复制

筛选返回的成员种类

可以调用Type的GetMembers,GetFields,GetMethods,GetProperties或者GetEvenents方法来查询一个类型的成员。在调用上面的任何一个方法时,都可以传递System.Reflection.BindingFlags枚举类型的一个实例,使用这个枚举类型目的是对这些方法返回的成员进行筛选。

注意:在返回一个成员集合的所有方法中, 都有一个不获取任何实参的重载版本。如果不传递BindingFlags实参,所有这些方法都返回公共成员,默认设置为BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static. (如果指定Public或NonPublic,那么必须同时指定Instance,否则不返回成员)。

以上就是Java中的程序集加载和反射机制是什么?的详细内容,更多请关注php中文网其它相关文章!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3