反射的作用:
1. 可以使用反射動態地建立型別的例項,將型別繫結到現有物件,或從現有物件中獲取型別
2. 應用程式需要在執行時從某個特定的程式集中載入乙個特定的型別,以便實現某個任務時可以用到反射。
3. 反射主要應用與類庫,這些類庫需要知道乙個型別的定義,以便提供更多的功能。
1 需要反射的dll
using system;
namespace webtest
public string writestring(string s)
//靜態函式
public static string writename(string s)
//不帶引數的函式
public string writenopara()}}
應用於反射的例子-在
aspnet頁面中加入以下函式:
public void test1()
); // 例項方法的呼叫
或:string s = (string)method.invoke(obj,object parametors = new object);
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 反射 動態載入dll簡單例子
假設已有元件classlibrarytest.dll,放置於程式目錄下。元件中classlibrarytest命名空間下有test類,類中有方法sum。下面示例就是動態載入元件並呼叫sum方法的簡例 1 static void main string args 2 classlibrarytest....
c 實現動態載入Dll
原理如下 1 利用反射進行動態載入和呼叫.assembly assembly assembly.loadfrom dllpath 利用dll的路徑載入,同時將此程式集所依賴的程式集載入進來,需後輟名.dll assembly.loadfile 只載入指定檔案,並不會自動載入依賴程式集.assmbly...
c 實現動態載入Dll
基本思路 1 利用反射進行動態載入和呼叫.assembly assembly assembly.loadfrom dllpath 利用dll的路徑載入,同時將此程式集所依賴的 程式集載入進來,需後輟名.dll assembly.loadfile 只載入指定檔案,並不會自動載入依賴程式集.assmbl...