C 反射機制

2021-07-28 05:54:15 字數 3629 閱讀 2085

使用assembly定義和引導程式集,載入在程式集清單中列出模組,以及從此程式集中查詢型別並建立該型別的例項。

使用module了解包含模組的程式集以及模組中的類等,還可以獲取在模組上定義的所有全域性方法或其他特定的非全域性方法。

使用constructorinfo了解建構函式的名稱、引數、訪問修飾符(如pulic 或private)和實現詳細資訊(如abstract或virtual)等。

使用methodinfo了解方法的名稱、返回型別、引數、訪問修飾符(如pulic 或private)和實現詳細資訊(如abstract或virtual)等。

使用fiedinfo了解欄位的名稱、訪問修飾符(如public或private)和實現詳細資訊(如static)等,並獲取或設定字段值。

使用eventinfo了解事件的名稱、事件處理程式資料型別、自定義屬性、宣告型別和反射型別等,新增或移除事件處理程式。

使用propertyinfo了解屬性的名稱、資料型別、宣告型別、反射型別和唯讀或可寫狀態等,獲取或設定屬性值。

使用parameterinfo了解引數的名稱、資料型別、是輸入引數還是輸出引數,以及引數在方法簽名中的位置等。

}//獲取型別資訊

protected void button1_click(object sender, eventargs e)

:", item.membertype, item));

response.write("

"); }

}

protected void button2_click(object sender, eventargs e)

}

protected void page_load(object sender, eventargs e)

);//呼叫方法

system.reflection

.methodinfo mi1 = t.getmethod("runjs");

mi1.invoke(t, new object );//呼叫方法

}

五:反射呼叫使用者/自定義控制項

protected

override

void

oninit(eventargs e)

private

void

createcontrol()

protected

void

button1_click(object sender, eventargs e)

));response.write(selectedvalue);

break;}}}}}

public

partial

class 反射 : system.web.ui.page

}public

static

class rawgenericfactory

}public

inte***ce itestinte***ce

public

class testclass : itestinte***ce

public

override

string

tostring()

public

string

dosomething()

}

[orm.table("testorm")]

public

class testorm

[orm.colum("username", dbtype.string)]

public

string username

[orm.colum("password", dbtype.string)]

public

string password

[orm.colum("createdtime", dbtype.datetime)]

public datetime createdtime

}protected

void

button3_click(object sender, eventargs e)

;orm.ormhelp h=new orm.ormhelp();

h.insert(t);

}namespace orm

public

tableattribute(string tablename)

////// 對映的表名(表的全名:模式名.表名)

/// public

string tablename

get}

}[attributeusageattribute(attributetargets.property, inherited = false, allowmultiple = false)]

public

class columattribute : attribute

public

columattribute(string columname)

: this()

public

columattribute(string columname, dbtype dbtype)

: this(columname)

//列名

public

virtual

string columname

get}

//描述一些特殊的資料庫型別

public dbtype dbtype

set }}

public

class ormhelp}}

//拼插入操作字串

C 反射機制

反射的概述 反射的定義 審查元資料並收集關於它的型別資訊的能力。元資料 編譯以後的最基本資料單元 就是一大堆的表,當編譯程式集或者模組時,編譯器會建立乙個類定義表,乙個字段定義表,和乙個方法定義表等。system.reflection命名空間包含的幾個類,允許你反射 解析 這些元資料表的 syste...

C 反射機制

反射的定義 審查元資料並收集關於它的型別資訊的能力。元資料 編譯以後的最基本資料單元 就是一大堆的表,當編譯程式集或者模組時,編譯器會建立乙個類定義表,乙個字段定義表,和乙個方法定義表等。system.reflection命名空間包含的幾個類,允許你反射 解析 這些元資料表的 system.refl...

C 反射機制

參考 本文的實現基於上述部落格中的 修改而來。本文實現的反射機制,其實就是根據字串,構造乙個類物件 classinfo是記錄類名與其相應的工廠函式 dynbase是實現反射機制的公共基類,每個繼承它的子類需要在建構函式中傳遞其類名的字串給dynbase的建構函式 templateclass dynb...