我們先看看一般的反射的動態方法尋找
下面為ms內建的例子ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemreflectionmethodbaseclassinvoketopic.htm
public class A
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
Console.WriteLine ("\nReflection.MethodInfo";
A MyA = new A();
B MyB = new B();
//Get the Type and MethodInfo
Type MyTypea = Type.GetType("A";
MethodInfo Mymethodinfoa = MyTypea.GetMethod("method";
Type MyTypeb = Type.GetType("B";
MethodInfo Mymethodinfob = MyTypeb.GetMethod("method";
//Get and display the Invoke method
Console.Write("\nFirst method - " + MyTypea.FullName +
" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write("\nSecond method - " + MyTypeb.FullName +
" returns " + Mymethodinfob.Invoke(MyB, null));
return 0;
}
}
下面是用介面查詢方法,執行個體建立對象,再執行執行個體對象
using System;
public interface IPoint
{
// return now class of shix interface
string ReturnNowClass();
}
上面檔案為 ClassSuc.cs 編輯為ClassSuc.dll 。
using System;
namespace ClassLib1
{
public class Class1: IPoint
{
public Class1()
{
}
public string ReturnNowClass()
{
return "weclone Execute ClassLib1 Class1";
}
}
}
將ClassSuc.dll的也添加到上面檔案,Class1實現了Ipoint介面
編輯檔案為ClassLib1.dll
using System;
namespace ClassLib2
{
public class Class2:IPoint
{
public Class2()
{
}
public string ReturnNowClass()
{
return "ClassLib2"+"Class2"+"weclone";
}
}
}
將ClassSuc.dll的也添加到上面檔案,Class2實現了Ipoint介面
編輯檔案為ClassLib2.dll
也許你已經看和做的不厭煩了,你可能要要問,你到底想講什嗎????
注意,將上面的三個dll copy 在 同一路徑下 這裡為“C:/test”
using System;
using System.Reflection;
class LoadExe
{
[STAThread]
static void Main(string[] args)
{
// Use the file name to load the assembly into the current application domain.
Assembly b;
b = Assembly.LoadFrom(@"C:/test/ClassSuc.dll";
Type[] mytypes = b.GetTypes();
// show b中的介面IPoint
foreach (Type t in mytypes)
{
Console.WriteLine (t.FullName);
}
MethodInfo Method = mytypes[0].GetMethod("ReturnNowClass";
//Get the method to call.
Assembly a ;
string k=Console.ReadLine();
//輸入大於10時,調用ClassLib1.dll的方法 否則調用ClassLib2的方法
if(Convert.ToInt32(k)>10)
a = Assembly.LoadFrom(@"C:/test/ClassLib1.dll";
else
a = Assembly.LoadFrom(@"C:/test/ClassLib2.dll";
Type[] types = a.GetTypes();
// show b中的ClassLib1.Class1或 ClassLib2.Class2
foreach (Type t in types)
{
Console.WriteLine (t.FullName);
}
// Create an instance of the HelloWorld class.
Object obj = Activator.CreateInstance(types[0]);
// Invoke the method.
Console.WriteLine(Method.Invoke(obj, null));
Console.ReadLine();
}
}
執行效果為:
Ipoint
這時要求輸入 輸入
13
繼續執行顯示
ClassLib1.Class1
weclone Execute ClassLib1 Class1
要求輸入時 輸入
5
繼續執行顯示
ClassLib2.Class2
weclone Execute ClassLib2 Class2
實現了什麼,通過介面動態載入程式集。
意義:反射機制實現動態插拔,只需更改設定檔和XCOPY相應的組件,
可以無須編譯就直接定製出一個特定系統
缺點: 效能衝擊 速度慢
有的人還要問,既然能夠動態載入程式集
那如何顯示卸載程式集 CLR不支援卸載程式集 但可以卸載AppDomain包含的所有的程式集。AppDomain.Unload 方法 卸載指定的應用程式定義域。
本還想寫一文章講述動態程式集 但自己理解不夠,又覺得意義不大所以把那些東西,自己也不想學那些東西(太浮躁的表現),所以提到這裡來。
動態程式集是編譯器或工具在運行時發出中繼資料和 MSIL,並可在磁碟上產生可移植可執行 (PE) 檔案 (不同於上面的那個動態載入程式集)
在運行時定義程式集,然後運行這些程式集並/或將它們儲存到磁碟。
在運行時定義新程式集中的模組,然後運行這些模組並/或將它們儲存到磁碟。
在運行時定義類型,建立這些類型的執行個體,並調用這些類型的方法。
程式集---》 模組---》類型—》執行個體的方法
具體見
ms-help://MS.NETFrameworkSDK.CHS/cpguidenf/html/cpconemittingdynamicassemblies.htm
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemappdomainclassdefinedynamicassemblytopic.htm