c 程式集及反射

2021-06-23 04:21:33 字數 1431 閱讀 1871

程式層次:

1.獲取當前應用程式域下的所有應用程式集

2.獲取乙個程式集下的類;

assembly assm = assembly.loadfile(@"d:\完成專案\2014-3-14\kongzhitai\xml\testlei\bin\debug\testlei.dll");

type types= assm.gettypes();

foreach (var type in types)

3.獲取乙個程式集下的類的屬性

assembly assm = assembly.loadfile(@"d:\完成專案\2014-3-14\kongzhitai\xml\testlei\bin\debug\testlei.dll");

type types= assm.gettypes();

foreach (var type in types)

}4.獲取類型別的幾種方法

(1)type type=typeof(person);

**:type type=typeof(peoson);

propertyinfo pros= type.getproperties();

foreach (var pro in pros)

(2)person p1=new person();

type type = p1.gettype();

peoson p1 = new peoson();

type type = p1.gettype();

propertyinfo pros = type.getproperties();

foreach (var pro in pros)

5.動態建立 引導程式集中 類的物件

建立介面專案:

public inte***ce jie

建立實現專案,實現上面介面:

public class people:jiekou1.jie

}主程式引用介面,用assembly獲取程式集中類,用activator.createinstance建立例項,並執行方法:

assembly assm = assembly.loadfile(@"d:\完成專案\2014-3-14\kongzhitai\xml\shixian1\bin\debug\shixian1.dll");

type type=assm.getexportedtypes().single();

object ibj= activator.createinstance((type));

jiekou1.jie jiekou=ibj as jiekou1.jie;

console.writeline(jiekou.sayhello());

activator.createinstance(type t)會動態呼叫類的public無參建構函式建立乙個物件,返回值就是建立的物件,如果類沒有無參建構函式就會報錯

C 程式集和反射

關於c 的程式集和反射,歷來都是兵家必爭之話題。首先我們來看程式集,程式集是 進行編譯是的乙個邏輯單元,把相關的 和型別進行組合,然後生成pe檔案 例如可執行檔案.exe和類庫檔案.dll 由於程式集在編譯後並不一定會生成單個檔案,而可能會生成多個物理檔案,甚至可能會生成分布在不同位置的多個物理檔案...

反射程式集

public void reflectassembly listtables,string iteguid,dictionary tablekeys 是建構函式中傳進去的引數的型別 在該示例中public timecopy listtables,string itemguid,dictionaryt...

C 反射建立物件(根據程式集)

反射動態建立物件 如果a,b,c,d都與執行 同乙個程式集.則可以這樣呼叫 system.reflection.assembly.getexecutingassembly createinstance 命名空間.類名 false 如 object o system.reflection.assemb...