目录

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

应用举例

类设计

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

运行的基本流程如下图:

C# GetMethod 方法应用实例-LMLPHP

用户通过访问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 核心方法其参数说明如下:

其调用结构如下图:

C# GetMethod 方法应用实例-LMLPHP

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

小结

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

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

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

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

04-25 17:11