C# GetMethod 方法应用实例

目录

关于 C# Type 类

GetMethod 方法应用

应用举例

类设计

类代码

小结


关于 C# Type 类

Type表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。调用 this.GetType() 方法得到Type对象,可获取成员信息,如方法名、变量名。更多学习请参照以下链接:

https://learn.microsoft.com/zh-cn/dotnet/api/system.type?view=net-8.0

本文以 API 模拟调用类应用实例介绍 Type.GetMethod 方法的实际应用。

GetMethod 方法应用

GetMethod 是获取当前 Type 的特定方法,具有多个重载,我们在这里介绍 GetMethod (string name, System.Reflection.BindingFlags bindingAttr)  即使用指定的绑定约束搜索指定方法。

其中 string name 表示要搜索的方法名称,System.Reflection.BindingFlags 枚举可见下表:

序号 筛选器标志 说明
1 BindingFlags.Instance 或 BindingFlags.Static  必须指定实例或静态方可有效返回
2 BindingFlags.Public 搜索当前 Type 中包含的公共方法
3 BindingFlags.NonPublic 搜索当前 Type 中包含的非公共方法 、私有方法、内部方法和保护方法
4 BindingFlags.FlattenHierarchy 在层次结构中的包括 public 和 protected 静态成员; private 继承类中的静态成员不包括在层次结构中
5 BindingFlags.IgnoreCase 忽略方法name的大小写进行搜索
6 BindingFlags.DeclaredOnly 如果只搜索 Type 声明的方法,则搜索只是继承的方法

应用举例

类设计

创建一个 CCAPI 类处理数据回应,该类设计如下:

序号 成员 类型 说明
1 HttpContext httpc = HttpContext.Current; 属性 System.Web.HttpContext,相当于被包装组合的网络请求,我们可以通过 HttpContext 访问诸如网络传递GET或POST提交的数据、文件等等
2 void init() 方法 处理请求,执行对应的接口功能并返回Json结果
3 string RunGetTypeMethod(string methodName, object[] paras) 方法 GetMethod 方法的应用,根据请求动作执行对应的方法

运行的基本流程如下图:

用户通过访问API地址,携带getType参数,参数值跟方法名称,后台 init() 方法通过 HttpContext.Current进行请求处理,执行 RunGetTypeMethod("methodA", null) 方法,查找 API 列表库中对应的方法名称 "methodA" ,并执行 string methodA() 方法,该方法返回 Json 处理结果。

类代码

示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Data;
using System.Web.SessionState;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
namespace CCAPI
{
    public class CCAPI
    {
        public HttpContext httpc = HttpContext.Current;
        
        public CCAPI()
        {
        }
        public void init()
        {
            string getType = httpc.Request["getType"];
            if (getType == null)
            {
                httpc.Response.Write("{\"errcode\":2,\"errmsg\":\"暂时不能提供服务,未提供合法getType值。\"}");
                httpc.Response.End();
                return;
            }
            string resultJson = "";
            resultJson = RunGetTypeMethod(getType, null);
            httpc.Response.Write(resultJson);
       }
       string methodA()
        {
            string result = "{\"errcode\":{0},\"errmsg\":\"methodA\"}";
            return result;
        }
       string methodB()
        {
            string result = "{\"errcode\":{0},\"errmsg\":\"methodB\"}";
            return result;
        }
       string methodC()
        {
            string result = "{\"errcode\":{0},\"errmsg\":\"methodC\"}";
            return result;
        }
        public string RunGetTypeMethod(string methodName, object[] paras)
        {
            string result = "";
            Type pageType = this.GetType();
            MethodInfo mInfo = pageType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
            if (mInfo != null)
            {
                result = "{\"errcode\":2,\"errmsg\":\"方法存在,但无法返回任何值。\"}";
                object user_rv = mInfo.Invoke(this, paras);
                if (mInfo.ReturnType != typeof(void))
                    if (user_rv.GetType() == typeof(string)) result = (string)user_rv;
            }
            else
            {
                result = "{\"errcode\":2,\"errmsg\":\"getType不是合法的API访问功能值\"}";
            }
            return result;
        }

    }
}

RunGetTypeMethod 核心方法其参数说明如下:

序号 参数 类型 说明
1 methodName string 要查找的字符串方法名称
2 object[] paras object[] 可传递方法要使用的参数列表,本应用里传递了 null 值。

其调用结构如下图:

调用 GetMethod 得到 MethodInfo 对象,然后 MethodInfo 再执行 Invoke 方法执行实例操作。

小结

GetMethod 方法的更多详情介绍,可参考如下链接:

https://learn.microsoft.com/zh-cn/dotnet/api/system.type.getmethod?view=net-8.0

类代码在这里仅为 GetMethod 方法讲解参考,实际的应用中还需要设计安全访问、日志跟踪等处理任务。

感谢您的阅读,希望本文能够对您有所帮助。

相关推荐

  1. MongoDB应用:forEach方法实际应用

    2024-04-25 19:08:05       38 阅读
  2. 机器学习:理论、方法应用实践

    2024-04-25 19:08:05       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 19:08:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 19:08:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 19:08:05       82 阅读
  4. Python语言-面向对象

    2024-04-25 19:08:05       91 阅读

热门阅读

  1. 如何在Chrome中设置无痕模式

    2024-04-25 19:08:05       34 阅读
  2. 云原生周刊:Kubernetes v1.30 发布 | 2024.4.22

    2024-04-25 19:08:05       64 阅读
  3. 多网卡IP配置netplan

    2024-04-25 19:08:05       179 阅读
  4. 前端实现批量下载并打包成ZIP文件

    2024-04-25 19:08:05       36 阅读
  5. Xshell常用命令大全

    2024-04-25 19:08:05       31 阅读
  6. tryhackme

    tryhackme

    2024-04-25 19:08:05      36 阅读