关于 c# type 类
type表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。调用 this.gettype() 方法得到type对象,可获取成员信息,如方法名、变量名。更多学习请参照以下链接:
本文以 api 模拟调用类应用实例介绍 type.getmethod 方法的实际应用。
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 方法执行实例操作。
小结
到此这篇关于c# getmethod方法的应用实例讲解的文章就介绍到这了,更多相关c# getmethod方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论