一般,反射用于动态获取对象的类型、属性和方法等信息。今天带你玩转反射,来汇总一下反射的各类常见操做,捡漏看看有没有你不知道的。bash
Type 类的 GetMembers 方法用来获取该类型的全部成员,包括方法和属性,可经过 BindingFlags 标志来筛选这些成员。函数
using System; using System.Reflection; using System.Linq; public class Program { public static voidMain() { var members = typeof(object).GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); foreach (var member in members) { Console.WriteLine($"{member.Name} is a {member.MemberType}"); } } }
输出:性能
GetType is a Method GetHashCode is a Method ToString is a Method Equals is a Method ReferenceEquals is a Method .ctor is a Constructor
GetMembers 方法也能够不传 BindingFlags,默认返回的是全部公开的成员。this
Type 类型的 GetMethod 方法用来获取该类型的 MethodInfo,而后可经过 MethodInfo 动态调用该方法。spa
对于非静态方法,须要传递对应的实例做为参数,示例:插件
class Program { public static void Main() { var str = "hello"; var method = str.GetType() .GetMethod("Substring", new[] {typeof(int), typeof(int)}); var result = method.Invoke(str, new object[] {0, 4}); // 至关于 str.Substring(0, 4) Console.WriteLine(result); // 输出:hell } }
对于静态方法,则对象参数传空,示例:code
var method = typeof(Math).GetMethod("Exp"); // 至关于 Math.Exp(2) var result = method.Invoke(null, new object[] {2}); Console.WriteLine(result); // 输出(e^2):7.38905609893065
若是是泛型方法,则还须要经过泛型参数来建立泛型方法,示例:orm
class Program { public static void Main() { // 反射调用泛型方法 MethodInfo method1 = typeof(Sample).GetMethod("GenericMethod"); MethodInfo generic1 = method1.MakeGenericMethod(typeof(string)); generic1.Invoke(sample, null); // 反射调用静态泛型方法 MethodInfo method2 = typeof(Sample).GetMethod("StaticMethod"); MethodInfo generic2 = method2.MakeGenericMethod(typeof(string)); generic2.Invoke(null, null); } } public class Sample { public void GenericMethod<T>() { //... } public static void StaticMethod<T>() { //... } }
使用反射动态建立一个类型的实例有多种种方式。最简单的一种是用 new()
条件声明。对象
若是在一个方法内须要动态建立一个实例,能够直接使用 new 条件声明,例如:接口
T GetInstance<T>() where T : new() { T instance = newT(); return instance; }
但这种方式适用场景有限,好比不适用于构造函数带参数的类型。
使用 Activator 类动态建立一个类的实例是最多见的作法,示例:
Type type = typeof(BigInteger); object result = Activator.CreateInstance(type); Console.WriteLine(result); // 输出:0 result = Activator.CreateInstance(type, 123); Console.WriteLine(result); // 输出:123
动态建立泛类型实例,须要先建立开放泛型(如List<>
),再根据泛型参数转换为具象泛型(如List<string>
),示例:
// 先建立开放泛型 Type openType = typeof(List<>); // 再建立具象泛型 Type[] tArgs = { typeof(string) }; Type target = openType.MakeGenericType(tArgs); // 最后建立泛型实例 List<string> result = (List<string>)Activator.CreateInstance(target);
若是你不知道什么是开放泛型和具象泛型,请看本文最后一节。
也能够经过反射构造器的方式动态建立类的实例,比上面使用 Activator 类要稍稍麻烦些,但性能要好些。示例:
ConstructorInfo c = typeof(T).GetConstructor(new[] { typeof(string) }); if (c == null) throw new InvalidOperationException("..."); T instance = (T)c.Invoke(new object[] { "test" });
若是你想建立某个类的实例的时候不执行构造函数和属性初始化,可使用 FormatterServices 的 GetUninitializedObject 方法。示例:
class Program { static void Main() { MyClass instance = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass)); Console.WriteLine(instance.MyProperty1); // 输出:0 Console.WriteLine(instance.MyProperty2); // 输出:0 } } public class MyClass { public MyClass(int val) { MyProperty1 = val < 1 ? 1 : val; } public int MyProperty1 { get; } public int MyProperty2 { get; set; } = 2; }
经过反射获取到对象的属性和方法后,若是你想经过强类型的方法来访问或调用,能够在中间加一层委托。这样的好处是有利于封装,调用者能够明确的知道调用时须要传什么参数。 好比下面这个方法,把 Math.Max 方法提取为一个强类型委托:
var tArgs = new Type[] { typeof(int), typeof(int) }; var maxMethod = typeof(Math).GetMethod("Max", tArgs); var strongTypeDelegate = (Func<int, int, int>)Delegate .CreateDelegate(typeof(Func<int, int, int>), null, maxMethod); Console.WriteLine("3 和 5 之间最大的是:{0}", strongTypeDelegate(3, 5)); // 输出:5
这个技巧也适用于属性,能够获取强类型的 Getter 和 Setter。示例:
var theProperty = typeof(MyClass).GetProperty("MyIntProperty"); // 强类型 Getter var theGetter = theProperty.GetGetMethod(); var strongTypeGetter = (Func<MyClass, int>)Delegate .CreateDelegate(typeof(Func<MyClass, int>), theGetter); var intVal = strongTypeGetter(target); // 相关于:target.MyIntProperty // 强类型 Setter var theSetter = theProperty.GetSetMethod(); var strongTypeSetter = (Action<MyClass, int>)Delegate .CreateDelegate(typeof(Action<MyClass, int>), theSetter); strongTypeSetter(target, 5); // 至关于:target.MyIntProperty = 5
如下是四个常见的场景示例。
示例一,找出一个类中标注了某个自定义特性(好比 MyAtrribute)的属性。
var props = type .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where(prop =>Attribute.IsDefined(prop, typeof(MyAttribute)));
示例二,找出某个属性的全部自定义特性。
var attributes = typeof(t).GetProperty("Name").GetCustomAttributes(false);
示例三,找出程序集全部标注了某个自定义特性的类。
static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly) { foreach(Type type inassembly.GetTypes()) { if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0) { yield return type; } } }
示例四,在运行时读取自定义特性的值
public static class AttributeExtensions { public static TValue GetAttribute<TAttribute, TValue>( this Type type, string MemberName, Func<TAttribute, TValue> valueSelector, bool inherit = false) where TAttribute : Attribute { var att = type.GetMember(MemberName).FirstOrDefault() .GetCustomAttributes(typeof(TAttribute), inherit) .FirstOrDefault() as TAttribute; if (att != null) { return valueSelector(att); } return default; } } // 使用: class Program { static void Main() { // 读取 MyClass 类的 MyMethod 方法的 Description 特性的值 var description = typeof(MyClass) .GetAttribute("MyMethod", (DescriptionAttribute d) => d.Description); Console.WriteLine(description); // 输出:Hello } } public class MyClass { [Description("Hello")] public void MyMethod() { } }
经过反射来动态实例化某个接口的全部实现类,经常使用于实现系统的插件式开发。好比在程序启动的时候去读取指定文件夹(如 Plugins)中的 dll 文件,经过反射获取 dll 中全部实现了某个接口的类,并在适当的时候将其实例化。大体实现以下:
interface IPlugin { string Description { get; } void DoWork(); }
某个在独立 dll 中的类:
class HelloPlugin : IPlugin { public string Description => "A plugin that says Hello"; public void DoWork() { Console.WriteLine("Hello"); } }
在你的系统启动的时候动态加载该 dll,读取实现了 IPlugin 接口的全部类的信息,并将其实例化。
public IEnumerable<IPlugin> InstantiatePlugins(string directory) { var assemblyNames = Directory.GetFiles(directory, "*.addin.dll") .Select(name => new FileInfo(name).FullName).ToArray(); foreach (var fileName assemblyNames) AppDomain.CurrentDomain.Load(File.ReadAllBytes(fileName)); var assemblies = assemblyNames.Select(System.Reflection.Assembly.LoadFile); var typesInAssembly = assemblies.SelectMany(asm =>asm.GetTypes()); var pluginTypes = typesInAssembly.Where(type => typeof (IPlugin).IsAssignableFrom(type)); return pluginTypes.Select(Activator.CreateInstance).Cast<IPlugin>(); }
前文提到了构造泛型和具象泛型,这里解释一下。大多时候咱们所说的泛型都是指构造泛型,有时候也被称为具象泛型。好比 List<int>
就是一个构造泛型,由于它能够经过 new 来实例化。相应的,List<>
泛型是非构造泛型,有时候也被称为开放泛型,它不能被实例化。开放泛型经过反射能够转换为任意的具象泛型,这一点前文有示例。
假如如今有一个泛型实例,出于某种需求,咱们想知道构建这个泛型实例须要用什么泛型参数。好比某人建立了一个 List<T>
泛型的实例,并把它做为参数传给了咱们的一个方法:
var myList = newList<int>(); ShowGenericArguments(myList);
咱们的方法签名是这样的:
public void ShowGenericArguments(object o)
这时,做为此方法的编写者,咱们并不知道这个 o 对象具体是用什么类型的泛型参数构建的。经过反射,咱们能够获得泛型实例的不少信息,其中最简单的就是判断一个类型是否是泛型:
public void ShowGenericArguments(object o) { if (o == null) return; Type t =o.GetType(); if (!t.IsGenericType) return; ... }
因为 List<>
自己也是泛型,因此上面的判断不严谨,咱们须要知道的是对象是否是一个构造泛型(List<int>
)。而 Type 类还提供了一些有用的属性:
typeof(List<>).IsGenericType // true typeof(List<>).IsGenericTypeDefinition // true typeof(List<>).IsConstructedGenericType// false typeof(List<int>).IsGenericType // true typeof(List<int>).IsGenericTypeDefinition // false typeof(List<int>).IsConstructedGenericType// true
IsConstructedGenericType
和 IsGenericTypeDefinition
分别用来判断某个泛型是否是构造泛型和非构造泛型。
再结合 Type 的 GetGenericArguments()
方法,就能够很容易地知道某个泛型实例是用什么泛型参数构建的了,例如:
static void ShowGenericArguments(object o) { if (o == null) return; Type t = o.GetType(); if (!t.IsConstructedGenericType) return; foreach (Type genericTypeArgument in t.GetGenericArguments()) Console.WriteLine(genericTypeArgument.Name); }
以上是关于反射的干货知识,都是从实际项目开发中总结而来,但愿对你的开发有帮助。