目录
Type表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。调用 this.GetType() 方法得到Type对象,可获取成员信息,如方法名、变量名。更多学习请参照以下链接:
https://learn.microsoft.com/zh-cn/dotnet/api/system.type?view=net-8.0
本文以 API 模拟调用类应用实例介绍 Type.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 方法讲解参考,实际的应用中还需要设计安全访问、日志跟踪等处理任务。
感谢您的阅读,希望本文能够对您有所帮助。