百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

「C#.NET 拾遗补漏」04:你必须知道的反射

yuyutoo 2024-11-12 14:57 32 浏览 0 评论

阅读本文大概需要 3 分钟。

通常,反射用于动态获取对象的类型、属性和方法等信息。今天带你玩转反射,来汇总一下反射的各种常见操作,捡漏看看有没有你不知道的。

获取类型的成员

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(#34;{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,默认返回的是所有公开的成员。

获取并调用对象的方法

Type 类型的 GetMethod 方法用来获取该类型的 MethodInfo,然后可通过 MethodInfo 动态调用该方法。

对于非静态方法,需要传递对应的实例作为参数,示例:

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
    }
}

对于静态方法,则对象参数传空,示例:

var method = typeof(Math).GetMethod("Exp");
// 相当于 Math.Exp(2)
var result = method.Invoke(null, new object[] {2});
Console.WriteLine(result); // 输出(e^2):7.38905609893065

如果是泛型方法,则还需要通过泛型参数来创建泛型方法,示例:

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 条件声明

如果在一个方法内需要动态创建一个实例,可以直接使用 new 条件声明,例如:

T GetInstance<T>() where T : new()
{
    T instance = newT();
    return instance;
}

但这种方式适用场景有限,比如不适用于构造函数带参数的类型。

使用 Activator 类

使用 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 类

如果你想创建某个类的实例的时候不执行构造函数和属性初始化,可以使用 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);
}

以上是关于反射的干货知识,都是从实际项目开发中总结而来,希望对你的开发有帮助。

相关推荐

墨尔本一华裔男子与亚裔男子分别失踪数日 警方寻人

中新网5月15日电据澳洲新快网报道,据澳大利亚维州警察局网站消息,22岁的华裔男子邓跃(Yue‘Peter’Deng,音译)失踪已6天,维州警方于当地时间13日发布寻人通告,寻求公众协助寻找邓跃。华...

网络交友须谨慎!美国犹他州一男子因涉嫌杀害女网友被捕

伊森·洪克斯克(图源网络,侵删)据美国广播公司(ABC)25日报道,美国犹他州一名男子于24日因涉嫌谋杀被捕。警方表示,这名男子主动告知警局,称其杀害了一名在网络交友软件上认识的25岁女子。雷顿警...

一课译词:来龙去脉(来龙去脉 的意思解释)

Mountainranges[Photo/SIPA]“来龙去脉”,汉语成语,本指山脉的走势和去向,现比喻一件事的前因后果(causeandeffectofanevent),可以翻译为“i...

高考重要考点:range(range高考用法)

range可以用作动词,也可以用作名词,含义特别多,在阅读理解中出现的频率很高,还经常作为完形填空的选项,而且在作文中使用是非常好的高级词汇。...

C++20 Ranges:现代范围操作(现代c++白皮书)

1.引言:C++20Ranges库简介C++20引入的Ranges库是C++标准库的重要更新,旨在提供更现代化、表达力更强的方式来处理数据序列(范围,range)。Ranges库基于...

学习VBA,报表做到飞 第二章 数组 2.4 Filter函数

第二章数组2.4Filter函数Filter函数功能与autofilter函数类似,它对一个一维数组进行筛选,返回一个从0开始的数组。...

VBA学习笔记:数组:数组相关函数—Split,Join

Split拆分字符串函数,语法Split(expression,字符,Limit,compare),第1参数为必写,后面3个参数都是可选项。Expression为需要拆分的数据,“字符”就是以哪个字...

VBA如何自定义序列,学会这些方法,让你工作更轻松

No.1在Excel中,自定义序列是一种快速填表机制,如何有效地利用这个方法,可以大大增加工作效率。通常在操作工作表的时候,可能会输入一些很有序的序列,如果一一录入就显得十分笨拙。Excel给出了一种...

Excel VBA入门教程1.3 数组基础(vba数组详解)

1.3数组使用数组和对象时,也要声明,这里说下数组的声明:'确定范围的数组,可以存储b-a+1个数,a、b为整数Dim数组名称(aTob)As数据类型Dimarr...

远程网络调试工具百宝箱-MobaXterm

MobaXterm是一个功能强大的远程网络工具百宝箱,它将所有重要的远程网络工具(SSH、Telnet、X11、RDP、VNC、FTP、MOSH、Serial等)和Unix命令(bash、ls、cat...

AREX:携程新一代自动化回归测试工具的设计与实现

一、背景随着携程机票BU业务规模的不断提高,业务系统日趋复杂,各种问题和挑战也随之而来。对于研发测试团队,面临着各种效能困境,包括业务复杂度高、数据构造工作量大、回归测试全量回归、沟通成本高、测试用例...

Windows、Android、IOS、Web自动化工具选择策略

Windows平台中应用UI自动化测试解决方案AutoIT是开源工具,该工具识别windows的标准控件效果不错,但是当它遇到应用中非标准控件定义的UI元素时往往就无能为力了,这个时候选择silkte...

python自动化工具:pywinauto(python快速上手 自动化)

简介Pywinauto是完全由Python构建的一个模块,可以用于自动化Windows上的GUI应用程序。同时,它支持鼠标、键盘操作,在元素控件树较复杂的界面,可以辅助我们完成自动化操作。我在...

时下最火的 Airtest 如何测试手机 APP?

引言Airtest是网易出品的一款基于图像识别的自动化测试工具,主要应用在手机APP和游戏的测试。一旦使用了这个工具进行APP的自动化,你就会发现自动化测试原来是如此简单!!连接手机要进行...

【推荐】7个最强Appium替代工具,移动App自动化测试必备!

在移动应用开发日益火爆的今天,自动化测试成为了确保应用质量和用户体验的关键环节。Appium作为一款广泛应用的移动应用自动化测试工具,为测试人员所熟知。然而,在不同的测试场景和需求下,还有许多其他优...

取消回复欢迎 发表评论: