C 反射例項講解

2021-04-25 17:25:51 字數 1047 閱讀 5643

1 建立用於反射使用的dll

新建乙個c#類庫專案,拷貝源**如下,編譯生成dll(假如dll的檔名是testreflect.dll)

1using system;

23namespace webtest412

13  public string writestring(string s)

14 

1718  /**

19  /// dsajkjflasjdfalksdjfaskfd

20  ///

21  ///

22  ///

23  public static string writename(string s)

24 

2728  public string writenopara()

29 

32 }

33}34

3536

2 應用於反射的例子

在aspnet頁面中加入以下函式:

1public void test1()

2  ); //例項方法的呼叫

13  

14    response.write(s+"

");15    method = type.getmethod("writename");//方法的名稱

16    s = (string)method.invoke(null,new string); //靜態方法的呼叫

17    response.write(s+"

");18

19    method = type.getmethod("writenopara");//無引數的例項方法

20    s = (string)method.invoke(obj,null);

21    response.write(s+"

");22    method = null;

23   }

24   catch(exception ex)

25  

28   finally

29  

34  }

C 反射例項講解

1 建立用於反射使用的dll 新建乙個c 類庫專案,拷貝源 如下,編譯生成dll 假如dll的檔名是testreflect.dll 1using system 23namespace webtest412 13 public string writestring string s 14 1718 1...

C 反射例項講解

1 建立用於反射使用的dll 新建乙個c 類庫專案,拷貝源 如下,編譯生成dll 假如dll的檔名是testreflect.dll using system namespace webtest public string writestring string s dsajkjflasjdfalksd...

C 反射機制和作用例項講解

第一件事就是 反射很慢,消耗效能。反射 可以通過程式集,型別,型別例項獲取該程式集內 所有型別 型別所有欄位 型別欄位的型別,方法,方法的訪問型別,引數和返回值等等。作用 1 json或xml解析,用於例項化乙個class類,當然需要該class類和jsondata或xml data的內容相匹配。評...