当前位置: 首页 > article >正文

C# MethodBase 类使用详解

总目录


前言

在C#编程中,反射(Reflection)是一种强大的机制,允许我们在运行时检查和操作类型的成员。MethodBase 类是.NET框架中 System.Reflection 命名空间下的一个抽象类,它是所有方法( MethodInfoConstructorInfo )的基类,包括实例方法和静态方法。通过 MethodBase,我们可以获取方法的元数据,如方法名称、返回类型、参数等。本文将详细讲解 MethodBase 类的使用方法、核心功能以及注意事项。


一、什么是 MethodBase 类?

1. 定义

MethodBase 类是 System.Reflection 命名空间中的一个抽象类,继承自 MemberInfo,是 MethodInfo(表示方法)和 ConstructorInfo(表示构造函数)的基类。通过 MethodBase,我们可以获取方法的元数据,如方法名称、返回类型、参数等。

2. 核心功能

  • 获取方法或构造函数的元数据:如方法名称、参数列表、返回类型、访问修饰符等。
  • 判断方法或构造函数的特性:如是否为静态方法、虚方法、抽象方法等。
  • 支持动态调用方法或构造函数(通过派生类 MethodInfoConstructorInfo)。

3. 与 MethodInfoConstructorInfo 的区别

  • MethodBase 是基类:提供通用方法和属性,但无法直接调用方法或构造函数。
  • MethodInfo 是派生类:提供 Invoke 方法等具体功能,用于操作方法。
  • ConstructorInfo 是派生类:用于操作构造函数,如创建对象实例:
    ConstructorInfo ctor = typeof(MyClass).GetConstructor(Type.EmptyTypes);
    object instance = ctor.Invoke(new object[] { });
    

4. MethodBase 的核心属性与方法

1)核心属性

属性名描述
Name获取方法或构造函数的名称。
DeclaringType获取声明该方法或构造函数的类型。
IsPublic判断方法或构造函数是否为公共的。
IsPrivate判断方法或构造函数是否为私有的。
IsStatic判断方法或构造函数是否为静态的。
IsVirtual判断方法是否为虚方法。
IsAbstract判断方法是否为抽象方法。
IsConstructor判断当前对象是否为构造函数。
ReflectedType获取用于获取 MethodBase 实例的类型。
Attributes获取方法或构造函数的特性。

2)核心方法

方法名描述
GetCustomAttributes获取应用到方法或构造函数的自定义特性。
GetParameters获取方法的参数信息(通过 MethodInfo 派生调用)。
Invoke动态调用方法。
GetMethod获取特定的方法信息。
GetCurrentMethod获取当前的方法信息。

二、MethodBase 的使用

1. 获取当前执行的方法

public void MyMethod()
{
    MethodBase currentMethod = MethodBase.GetCurrentMethod();
    Console.WriteLine($"Current Method: {currentMethod.Name}"); // 输出: MyMethod
}

2. 获取方法的基本信息

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        MethodBase currentMethod = MethodBase.GetCurrentMethod();
        Console.WriteLine("方法名称:" + currentMethod.Name);
        Console.WriteLine("声明类型:" + currentMethod.DeclaringType);
        Console.WriteLine("反射类型:" + currentMethod.ReflectedType);
        Console.WriteLine("是否为静态方法:" + currentMethod.IsStatic);
        Console.WriteLine("是否为公共方法:" + currentMethod.IsPublic);
    }
}

3. 获取方法参数

public class MyClass
{
    public void MyMethod(string name, int age = 30, bool isStudent = false)
    {
        Console.WriteLine($"Name: {name}, Age: {age}, IsStudent: {isStudent}");
    }
}
internal class Program
{
    static void Main(string[] args)
    {
        MethodInfo method = typeof(MyClass).GetMethod("MyMethod");
        ParameterInfo[] parameterInfos= method.GetParameters();
        foreach (var item in parameterInfos)
        {
            Console.WriteLine($"类型:{item.ParameterType}\t 名称:{item.Name}");
        }
    }
}

输出结果:

类型:System.String      名称:name
类型:System.Int32       名称:age
类型:System.Boolean     名称:isStudent

3. 获取MethodBase 对象

由于 MethodBaseMethodInfoConstructorInfo 的基类,因此通过GetMethod / GetMethods 以及 GetConstructor/GetConstructors 都可以间接获取得到MethodBase

1)GetMethod 方法

GetMethod 方法:用于获取单个方法,可以根据方法名称和参数类型精确匹配。

Type personType = typeof(Person);
MethodInfo sayHelloMethod = personType.GetMethod("SayHello", new Type[] { typeof(string) });

如果需要使用到 派生类 MethodInfo中的功能,还是推荐使用MethodInfo

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(Program);
        MethodInfo method = type.GetMethod("MyMethod");
        //同样可以使用MethodBase接收,但可以导致无法使用MethodInfo中的功能,如ReturnType
        //MethodInfo method = type.GetMethod("MyMethod");
        Console.WriteLine("方法名称:" + method.Name);
        Console.WriteLine("返回类型:" + method.ReturnType);
        Console.WriteLine("参数数量:" + method.GetParameters().Length);
    }

    public static void MyMethod(int param1, string param2)
    {
        // 方法体
    }
}

2)GetMethods 方法

GetMethods 方法:用于获取所有方法,返回一个 MethodInfo 对象的数组。

  MethodInfo[] methods = personType.GetMethods();
 
 // 同样可以使用 MethodBase 接收
 // MethodBase[] methods = personType.GetMethods();  

4. 动态调用方法(通过 MethodInfo

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type type = typeof(Program);
        MethodInfo method = type.GetMethod("MyMethod");
        object result = method.Invoke(null, new object[] { 10, "Hello" });
        Console.WriteLine("方法返回值:" + result);
    }

    public static int MyMethod(int param1, string param2)
    {
        Console.WriteLine("参数1:" + param1);
        Console.WriteLine("参数2:" + param2);
        return param1;
    }
}

三、注意事项与最佳实践

  • 静态方法与实例方法调用:对于静态方法,调用 Invoke 时第一个参数传 null,对于实例方法,传入实例对象。
  • 参数匹配:调用 Invoke 方法时,必须确保传递的参数类型和数量与方法的签名匹配,否则会抛出异常。
  • 访问修饰符:调用非公共方法时,需要使用 BindingFlags 指定访问修饰符,否则 GetMethod 方法可能无法找到该方法。
  • 性能开销:反射操作比直接调用慢,高频场景需谨慎使用,或通过缓存 MethodBase 实例优化。
  • 可读性和维护性:过度使用反射可能导致代码可读性下降和维护难度增加。

结语

回到目录页:C#/.NET 知识汇总
希望以上内容可以帮助到大家,如文中有不对之处,还请批评指正。


参考资料:

  • .NET 反射基础教程
  • MethodBase 类文档
  • MethodInfo vs. ConstructorInfo

http://www.kler.cn/a/594982.html

相关文章:

  • acwing1295. X的因子链
  • CMake 函数和宏
  • 嵌入式软件单元测试的必要性、核心方法及工具深度解析
  • 在 Windows 系统下,将 FFmpeg 编译为 .so 文件
  • Touch Diver:Weart为XR和机器人遥操作专属设计的触觉反馈动捕手套
  • 对敏捷研发的反思,是否真是灵丹妙药?
  • HTTPS 加密过程详解
  • 【SpringBoot】MorningBox小程序的完整后端接口文档
  • 3.20【L】algorithm
  • 「Java EE开发指南」用MyEclipse开发EJB 3无状态会话Bean(一)
  • HTML5响应式使用css媒体查询
  • teaming技术
  • Python深浅拷贝
  • 【QA】装饰模式在Qt中有哪些运用?
  • 服务器——报错解决:移动文件时,bash: /usr/bin/mv: Argument list too long
  • Java基础关键_027_IO流(五)
  • 软考-软件设计师-程序设计语言
  • 数据结构——顺序栈seq_stack
  • 力扣刷题——143.重排链表
  • 多数据源@DS和@Transactional踩坑之路