反射的用途简要介绍
反射的用途大体总结如下,我们会在下面详细的进行介绍。
(1)使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例。
(2)使用Module了解包含模块的程序集以及模块中的类等,还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。
(3)使用ConstructorInfo了解构造函数的名称、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。
(4)使用MethodInfo了解方法的名称、返回类型、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。
(5)使用FiedInfo了解字段的名称、访问修饰符(如public或private)和实现详细信息(如static)等,并获取或设置字段值。
(6)使用EventInfo了解事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等,添加或移除事件处理程序
(7)使用PropertyInfo了解属性的名称、数据类型、声明类型、反射类型和只读或可写状态等,获取或设置属性值。
(8)使用ParameterInfo了解参数的名称、数据类型、是输入参数还是输出参数,以及参数在方法签名中的位置等。
反射用到的主要类:
System.Type 类–通过这个类可以访问任何给定数据类型的信息。
System.Reflection.Assembly类–它可以用于访问给定程序集的信息,或者把这个程序集加载到程序中。
System.Type类:System.Type 类对于反射起着核心的作用。但它是一个抽象的基类,Type有与每种数据类型对应的派生类,我们使用这个派生类的对象的方法、字段、属性来查找有关该类型的所有信息。
Type类的属性:
Name 数据类型名
FullName 数据类型的完全限定名(包括命名空间名)
Namespace 定义数据类型的命名空间名
IsAbstract 指示该类型是否是抽象类型
IsArray 指示该类型是否是数组
IsClass 指示该类型是否是类
IsEnum 指示该类型是否是枚举
IsInterface 指示该类型是否是接口
IsPublic 指示该类型是否是公有的
IsSealed 指示该类型是否是密封类
IsValueType 指示该类型是否是值类型
Type类的方法:
GetConstructor(), GetConstructors():返回
ConstructorInfo类型,用于取得该类的构造函数的信息
GetEvent(), GetEvents():返回EventInfo类型,用于取得该类的事件的信息
GetField(), GetFields():返回FieldInfo类型,用于取得该类的字段(成员变量)的信息
GetInterface(), GetInterfaces():返回InterfaceInfo类型,用于取得该类实现的接口的信息
GetMember(), GetMembers():返回MemberInfo类型,用于取得该类的所有成员的信息
GetMethod(), GetMethods():返回MethodInfo类型,用于取得该类的方法的信息
GetProperty(), GetProperties():返回PropertyInfo类型,用于取得该类的属性的信息可以调用这些成员,其方式是调用Type的InvokeMember()方法,或者调用MethodInfo, PropertyInfo和其他类的Invoke()方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Object obje = new test();
Type types = obje.GetType();
Console.WriteLine(types.Name);
Console.WriteLine(types.FullName);
//设置属性值
types.GetProperty("age").SetValue(obje, 23);
//获取属性值
Console.WriteLine(types.GetProperty("age").GetValue(obje));
}
}
class test
{
public string s1;
public int age { get; set; }
public string returnValue()
{
return $"age is {age}";
}
}
}
使用反射加载程序集
新建一个类库, 主程序以及需要反射的程序都会引用这个dll,主要是用到了共同的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace based
{
public class baseClass
{
public string Test { get; set; }
public baseClass(string test)
{
this.Test = test + "hhhh";
}
public string test()
{
return $"{this.Test}";
}
}
}
反射的dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace reflect
{
public class Class1
{
public based.baseClass Test { get; set; }
public Class1(based.baseClass test)
{
this.Test = test;
}
public string hhhhh()
{
return Test.test();
}
}
}
运行反射
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
based.baseClass baseClass = new based.baseClass("12345");
Assembly assembly = Assembly.LoadFile(Path.Combine(Environment.CurrentDirectory, "reflect.dll"));
Type type = assembly.GetTypes().First(x => x.IsClass && x.Name == "Class1");
Object obj = assembly.CreateInstance(type.FullName,false, BindingFlags.Default,null,new object[] { baseClass},null,null);
string s= type.GetMethod("hhhhh").Invoke(obj,new object[] { }).ToString();
}
}
https://zhuanlan.zhihu.com/p/41282759