C# GetMethod方法的應(yīng)用實(shí)例講解
關(guān)于 C# Type 類
Type表示類型聲明:類類型、接口類型、數(shù)組類型、值類型、枚舉類型、類型參數(shù)、泛型類型定義,以及開(kāi)放或封閉構(gòu)造的泛型類型。調(diào)用 this.GetType() 方法得到Type對(duì)象,可獲取成員信息,如方法名、變量名。更多學(xué)習(xí)請(qǐng)參照以下鏈接:
本文以 API 模擬調(diào)用類應(yīng)用實(shí)例介紹 Type.GetMethod 方法的實(shí)際應(yīng)用。
GetMethod 方法應(yīng)用
GetMethod 是獲取當(dāng)前 Type 的特定方法,具有多個(gè)重載,我們?cè)谶@里介紹 GetMethod (string name, System.Reflection.BindingFlags bindingAttr) 即使用指定的綁定約束搜索指定方法。
其中 string name 表示要搜索的方法名稱,System.Reflection.BindingFlags 枚舉可見(jiàn)下表:
| 序號(hào) | 篩選器標(biāo)志 | 說(shuō)明 |
|---|---|---|
| 1 | BindingFlags.Instance 或 BindingFlags.Static | 必須指定實(shí)例或靜態(tài)方可有效返回 |
| 2 | BindingFlags.Public | 搜索當(dāng)前 Type 中包含的公共方法 |
| 3 | BindingFlags.NonPublic | 搜索當(dāng)前 Type 中包含的非公共方法 、私有方法、內(nèi)部方法和保護(hù)方法 |
| 4 | BindingFlags.FlattenHierarchy | 在層次結(jié)構(gòu)中的包括 public 和 protected 靜態(tài)成員; private 繼承類中的靜態(tài)成員不包括在層次結(jié)構(gòu)中 |
| 5 | BindingFlags.IgnoreCase | 忽略方法name的大小寫(xiě)進(jìn)行搜索 |
| 6 | BindingFlags.DeclaredOnly | 如果只搜索 Type 聲明的方法,則搜索只是繼承的方法 |
應(yīng)用舉例
類設(shè)計(jì)
創(chuàng)建一個(gè) CCAPI 類處理數(shù)據(jù)回應(yīng),該類設(shè)計(jì)如下:
| 序號(hào) | 成員 | 類型 | 說(shuō)明 |
|---|---|---|---|
| 1 | HttpContext httpc = HttpContext.Current; | 屬性 | System.Web.HttpContext,相當(dāng)于被包裝組合的網(wǎng)絡(luò)請(qǐng)求,我們可以通過(guò) HttpContext 訪問(wèn)諸如網(wǎng)絡(luò)傳遞GET或POST提交的數(shù)據(jù)、文件等等 |
| 2 | void init() | 方法 | 處理請(qǐng)求,執(zhí)行對(duì)應(yīng)的接口功能并返回Json結(jié)果 |
| 3 | string RunGetTypeMethod(string methodName, object[] paras) | 方法 | GetMethod 方法的應(yīng)用,根據(jù)請(qǐng)求動(dòng)作執(zhí)行對(duì)應(yīng)的方法 |
運(yùn)行的基本流程如下圖:

用戶通過(guò)訪問(wèn)API地址,攜帶getType參數(shù),參數(shù)值跟方法名稱,后臺(tái) init() 方法通過(guò) HttpContext.Current進(jìn)行請(qǐng)求處理,執(zhí)行 RunGetTypeMethod("methodA", null) 方法,查找 API 列表庫(kù)中對(duì)應(yīng)的方法名稱 "methodA" ,并執(zhí)行 string methodA() 方法,該方法返回 Json 處理結(jié)果。
類代碼
示例代碼如下:
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\":\"暫時(shí)不能提供服務(wù),未提供合法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\":\"方法存在,但無(wú)法返回任何值。\"}";
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訪問(wèn)功能值\"}";
}
return result;
}
}
}RunGetTypeMethod 核心方法其參數(shù)說(shuō)明如下:
| 序號(hào) | 參數(shù) | 類型 | 說(shuō)明 |
|---|---|---|---|
| 1 | methodName | string | 要查找的字符串方法名稱 |
| 2 | object[] paras | object[] | 可傳遞方法要使用的參數(shù)列表,本應(yīng)用里傳遞了 null 值。 |
其調(diào)用結(jié)構(gòu)如下圖:

調(diào)用 GetMethod 得到 MethodInfo 對(duì)象,然后 MethodInfo 再執(zhí)行 Invoke 方法執(zhí)行實(shí)例操作。
小結(jié)
到此這篇關(guān)于C# GetMethod方法的應(yīng)用實(shí)例講解的文章就介紹到這了,更多相關(guān)C# GetMethod方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法(深度復(fù)制)
這篇文章主要介紹了C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法,使用?BinaryFormatter?將原始?List?序列化為字節(jié)流,然后再反序列化得到新的?List,實(shí)現(xiàn)了深度復(fù)制,需要的朋友可以參考下2024-03-03
C#使用Thrift作為RPC框架入門(mén)詳細(xì)教程
這篇文件我們講了從0到1使用thrift框架的方法,也講了一些該框架的基本知識(shí),本文將詳細(xì)介紹 Thrift 在C#語(yǔ)言下的使用方式,并且提供豐富的實(shí)例代碼加以解釋說(shuō)明,幫助使用者快速構(gòu)建服務(wù),感興趣的朋友一起看看吧2021-11-11
實(shí)例詳解C#實(shí)現(xiàn)http不同方法的請(qǐng)求
本篇文章給大家分享了C#實(shí)現(xiàn)http不同方法的請(qǐng)求的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,有需要的朋友參考下。2018-07-07
C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)
這篇文章主要為大家詳細(xì)介紹了C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
詳解C#編程中異常的創(chuàng)建和引發(fā)以及異常處理
這篇文章主要介紹了C#編程中異常的創(chuàng)建和引發(fā)以及異常處理,文中介紹了Catch塊和Finally塊等基本的異常處理要點(diǎn),需要的朋友可以參考下2016-02-02

