C 反射例項講解

2021-04-18 21:28:11 字數 832 閱讀 1366

1 建立用於反射使用的dll

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

using system;

namespace webtest

public string writestring(string s)

/** /// dsajkjflasjdfalksdjfaskfd

///

/// ///

public static string writename(string s)

public string writenopara()

}}

2 應用於反射的例子

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

public void test1()

); //例項方法的呼叫

response.write(s+"

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

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

response.write(s+"

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

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

response.write(s+"

"); method = null;

}catch(exception ex)

finally

}

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 1using system 23namespace webtest412 13 public string writestring string s 14 1718 1...

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

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