当前位置: 代码网 > it编程>编程语言>C# > C# GetMethod方法的应用实例讲解

C# GetMethod方法的应用实例讲解

2024年05月26日 C# 我要评论
关于 c# type 类type表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。调用 this.gettype() 方法得到type对

关于 c# type 类

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

https://learn.microsoft.com

本文以 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 方法执行实例操作。

小结

到此这篇关于c# getmethod方法的应用实例讲解的文章就介绍到这了,更多相关c# getmethod方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com