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

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

調(diào)用 GetMethod 得到 MethodInfo 對象,然后 MethodInfo 再執(zhí)行 Invoke 方法執(zhí)行實例操作。
小結(jié)
到此這篇關(guān)于C# GetMethod方法的應(yīng)用實例講解的文章就介紹到這了,更多相關(guān)C# GetMethod方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#?中?List?與?List?多層嵌套不改變原值的實現(xiàn)方法(深度復(fù)制)
這篇文章主要介紹了C#?中?List?與?List?多層嵌套不改變原值的實現(xiàn)方法,使用?BinaryFormatter?將原始?List?序列化為字節(jié)流,然后再反序列化得到新的?List,實現(xiàn)了深度復(fù)制,需要的朋友可以參考下2024-03-03
詳解C#編程中異常的創(chuàng)建和引發(fā)以及異常處理
這篇文章主要介紹了C#編程中異常的創(chuàng)建和引發(fā)以及異常處理,文中介紹了Catch塊和Finally塊等基本的異常處理要點,需要的朋友可以參考下2016-02-02

