国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

使用C#進行Reflection編程

2024-07-21 02:19:35
字體:
來源:轉載
供稿:網友

代碼如下:

using system;
using system.reflection;
using system.reflection.emit ;

public class testreflection {
private string file = @"testreflection.exe";

static void main(string[] args) {
testreflection test = new testreflection();
test.displaymodules();
test.displaytypes();
test.displaymethods();
test.invokestaticmethod();
test.invokemethod();
}
//顯示所有模塊名
public void displaymodules() {
console.writeline("display modules ...");
assembly assembly = assembly.loadfrom(file);
module[] modules = assembly.getmodules();
foreach( module module in modules ) {
console.writeline("module name:" + module.name );
}
}
//顯示所有類型名
public void displaytypes() {
console.writeline("display types ...");
assembly assembly = assembly.loadfrom(file);
type[] types = assembly.gettypes();
foreach( type type in types ) {
console.writeline("type name:" + type.fullname );
}
}
//先是一個類型中的所有方法名
public void displaymethods() {
console.writeline("display methods in testreflection type ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testreflection");
methodinfo[] methods = type.getmethods();
foreach(methodinfo method in methods) {
console.writeline("method name:" + method.name);
}
}
//調用一個類中的靜態方法
public void invokestaticmethod() {
console.writeline("invoke static method ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testsubclass");
type.invokemember ("sayhello", bindingflags.invokemethod,null,null,new object[]{});
}
//調用一個類中的非靜態方法
public void invokemethod() {
console.writeline("invoke method ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testsubclass");
object obj = activator.createinstance(type);
testclass tc = (testclass)obj ;
type.invokemember ("test1", bindingflags.invokemethod,null,tc,new object[]{});
type.invokemember ("test2", bindingflags.invokemethod,null,tc,new object[]{});
}
}
public interface testclass {
void test1();
void test2();
}
public class testsubclass : testclass{
public void test1() {
console.writeline("this is testsubclass.test1");
}
public void test2() {
console.writeline("this is testsubclass.test2");
}
public static void sayhello() {
console.writeline("this is testsubclass.sayhello");
}
}

編譯運行后輸出:

display modules ...
module name:testreflection.exe
display types ...
type name:testreflection
type name:testclass
type name:testsubclass
display methods in testreflection type ...
method name:gethashcode
method name:equals
method name:tostring
method name:displaymodules
method name:displaytypes
method name:displaymethods
method name:invokestaticmethod
method name:invokemethod
method name:test2
method name:gettype
invoke static method ...
this is testsubclass.sayhello
invoke method ...
this is testsubclass.test1
this is testsubclass.test2




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连平县| 昂仁县| 西畴县| 萍乡市| 新建县| 台东市| 大田县| 禹城市| 通渭县| 隆化县| 汉寿县| 慈利县| 冷水江市| 娄烦县| 阿荣旗| 张北县| 门源| 民乐县| 寿光市| 东安县| 中宁县| 深水埗区| 沙雅县| 巫溪县| 太谷县| 定州市| 章丘市| 永康市| 乌兰浩特市| 新蔡县| 韩城市| 白朗县| 宁晋县| 三原县| 靖远县| 康定县| 柞水县| 民和| 石嘴山市| 芮城县| 江源县|