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 枚举可见下表:

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

应用举例

类设计

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

序号成员类型说明
1HttpContext httpc = HttpContext.Current;属性System.Web.HttpContext,相当于被包装组合的网络请求,我们可以通过 HttpContext 访问诸如网络传递GET或POST提交的数据、文件等等
2void init()方法处理请求,执行对应的接口功能并返回Json结果
3string 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 核心方法其参数说明如下:

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

其调用结构如下图:

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

小结

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

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

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

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

  • 27
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初九之潜龙勿用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值