C 反射之呼叫方法談

2022-01-30 07:16:10 字數 3544 閱讀 5832

反射的定義

反射提供了描述程式集、模組和型別的物件(type 型別)。 可以使用反射動態建立型別的例項,將型別繫結到現有物件,或從現有物件獲取型別並呼叫其方法或訪問其字段和屬性。 如果**中使用了特性,可以利用反射來訪問它們。------摘自msdn

自我理解

看到反射二字,自然而然的會想到,小時候拿著一面鏡子,反射陽光玩。其實

反射就好比一面鏡子,通過它我們能在不顯示引用程式集的情況下,一窺程式集內的「風景」。

利用好反射這把利器,我們在開發中就能體會到荀老夫子所言的,君子性非異也,善假於物也

通過反射呼叫類的建構函式(有參/無參)

通過反射呼叫類的靜態方法(有參/無參)

通過反射呼叫類的非靜態方法(有參/無參)

通過反射呼叫類的含有ref引數的方法

**示例:

1

namespace

testdll214

15//

有參建構函式

16public testclass(int

i)17

2021

//無參方法

22public

intadd()

2326

27//

ref引數的方法

28public

void exchange(ref

int a, ref

intb)

2934

35//

靜態有參方法

36public

static

string sayhi(string

name)

3740

41public

static

int addvalue(int

objsints)

4248

return

temp;49}

50}51 }

view code

通過反射呼叫**示例:

static

void main(string

args)

);//呼叫有參建構函式

constructorinfo paramconstructor = testclass.getconstructor(new type );

object testclassobject2 = paramconstructor.invoke(new

object );

#region 呼叫非靜態方法methodinfo addmethod = testclass.getmethod("

add"

);

object addvalue = addmethod.invoke(instance, new object);

#endregion

#region 呼叫靜態有參方法methodinfo sayhimethod = testclass.getmethod("

sayhi");

object sayhi = sayhimethod.invoke(null, new object );

#endregion

#region 呼叫含有ref引數的方法methodinfo exchange = testclass.getmethod("

exchange");

var objs = new

object[2

]; objs[

0] = 5

; objs[

1] = 6

; console.writeline(

"objs[0]=\nobjs[1]=

", objs[0], objs[1

]);

object retvalue =exchange.invoke(instance, objs);

#endregion

#region 呼叫引數為陣列的方法methodinfo addvalueinfo = testclass.getmethod("

addvalue");

var ints = new

int ;

object obj = addvalueinfo.invoke(null, new object);

#endregion

console.writeline(

"methodinfo.invoke() example\n");

console.writeline(

"testclass.add() returned:

", addvalue);

console.writeline(

"testclass.sayhi() returned:

", sayhi);

console.writeline(

"testclass.exchange(ref int a,ref int b) returned:

", retvalue);

console.writeline(

"objs[0]=\nobjs[1]=

", objs[0], objs[1

]); console.writeline(

"addvalue(int objsints) result:

", obj);

}

在此對**中紅色加粗的方法做注(摘自msdn):

methodbase.invoke 方法 (object, object)

使用指定的引數呼叫當前例項所表示的方法或建構函式。

語法public object invoke(

object obj,

object parameters

引數obj

型別:system.object

對其呼叫方法或建構函式的物件。 如果方法是靜態的,則忽略此引數。 如果建構函式是靜態的,則此引數必須為 null 或定義該建構函式的類的例項。

若要使用其 methodinfo 物件來呼叫靜態方法,請將 null 傳遞給 obj。

parameters

型別:system.object

呼叫的方法或建構函式的引數列表。 這是乙個物件陣列,這些物件與要呼叫的方法或建構函式的引數具有相同的數量、順序和型別。 如果沒有任何引數,則 parameters 應為 null。

如果此例項所表示的方法或建構函式採用 ref 引數(在 visual basic 中為 byref),使用此函式呼叫該方法或建構函式時,該引數不需要任何特殊屬性。 如果此陣列中的物件未用值來顯式初始化,則該物件將包含該物件型別的預設值。 對於引用型別的元素,該值為 null。 對於值型別的元素,該值為 0、0.0 或 false,具體取決於特定的元素型別。

返回值型別:system.object

乙個物件,包含被呼叫方法的返回值,如果呼叫的是建構函式,則為 null。

C 通過反射呼叫方法

用反射呼叫方法 常用於軟體架構中 假如你定義好了基類和介面,其他人使用基類派生出新的方法,你在不知道有多少類會使用此基類派生,但是你需要呼叫所有的派生類的方法時,就可以這樣,直接遍歷相同命名空間中的由基類派生的所有類,然後例項化所有類,呼叫所有方法 using system using system...

C 反射呼叫方法與特性呼叫方法

在一些高階的開源專案中通常會使用到反射和打特性,如果對於c 初級的程式設計師第一眼看到肯定一臉懵逼,我以前也是這樣過來的所以今天公司沒啥事情可以幹,就寫一下筆記 unity 開源專案et 以前看et專案時只知道原理不知其中的 所以今天自已實現一下原理 中就使用了這種方式實現。在此我解析一下反射與自定...

C 的反射機制呼叫方法

net的反射 reflection 是非常完善和強大的,例如有名的.net反編譯工具red gate s net reflector就是使用了.net自身的反射機制,這裡有乙個比較簡單的例項 使用控制台程式 看看.net中如何使用反射。using system using system.reflec...