• 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 处理结果。

    类代码

    示例代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Web;
    7. using System.Data;
    8. using System.Web.SessionState;
    9. using System.Collections;
    10. using System.Data.SqlClient;
    11. using System.IO;
    12. using System.Reflection;
    13. namespace CCAPI
    14. {
    15. public class CCAPI
    16. {
    17. public HttpContext httpc = HttpContext.Current;
    18. public CCAPI()
    19. {
    20. }
    21. public void init()
    22. {
    23. string getType = httpc.Request["getType"];
    24. if (getType == null)
    25. {
    26. httpc.Response.Write("{\"errcode\":2,\"errmsg\":\"暂时不能提供服务,未提供合法getType值。\"}");
    27. httpc.Response.End();
    28. return;
    29. }
    30. string resultJson = "";
    31. resultJson = RunGetTypeMethod(getType, null);
    32. httpc.Response.Write(resultJson);
    33. }
    34. string methodA()
    35. {
    36. string result = "{\"errcode\":{0},\"errmsg\":\"methodA\"}";
    37. return result;
    38. }
    39. string methodB()
    40. {
    41. string result = "{\"errcode\":{0},\"errmsg\":\"methodB\"}";
    42. return result;
    43. }
    44. string methodC()
    45. {
    46. string result = "{\"errcode\":{0},\"errmsg\":\"methodC\"}";
    47. return result;
    48. }
    49. public string RunGetTypeMethod(string methodName, object[] paras)
    50. {
    51. string result = "";
    52. Type pageType = this.GetType();
    53. MethodInfo mInfo = pageType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
    54. if (mInfo != null)
    55. {
    56. result = "{\"errcode\":2,\"errmsg\":\"方法存在,但无法返回任何值。\"}";
    57. object user_rv = mInfo.Invoke(this, paras);
    58. if (mInfo.ReturnType != typeof(void))
    59. if (user_rv.GetType() == typeof(string)) result = (string)user_rv;
    60. }
    61. else
    62. {
    63. result = "{\"errcode\":2,\"errmsg\":\"getType不是合法的API访问功能值\"}";
    64. }
    65. return result;
    66. }
    67. }
    68. }

    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 方法讲解参考,实际的应用中还需要设计安全访问、日志跟踪等处理任务。

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

  • 相关阅读:
    DBCP 与 C3P0连接池
    前端调试入门
    中石化/中石油 油卡充值接口API文档分享
    数据库(mysql)之用户管理
    考研:研究生考试(五天学完)之《线性代数与空间解析几何》研究生学霸重点知识点总结之第一课行列式
    直播回顾 | 论道原生:云原生大数据建设实践
    Android 14 系统启动流程 之 启动init进程、启动Zygote进程
    软件定义的云安全实践
    Redisson-lock看门狗原理
    【地铁上的面试题】--基础部分--数据结构与算法--数组和链表
  • 原文地址:https://blog.csdn.net/michaelline/article/details/138152057