我們先來看.net中與本幾個重要的概念:
- System.AppDomain 類包含幾種重載的 Load 方法。儘管這些方法可用於將任何程序集成功地加載至當前的或新的應用程序域,但它們主要還是用於 COM 交互操作。您也可以使用 CreateInstance 方法加載程序集。
- System.Reflection.Assembly 類包含兩種靜態重載方法:Load 和 LoadFrom。這兩種方法因加載上下文而異。
namespace dy_loadAsse
{
class testclass
{
[STAThread]
static void Main(string[] args)
{
OutUse test=new OutUse();
test.Output();
Console.ReadLine();
}
}
class OutUse
{
public OutUse()
{
}
public void Output()
{
Console.WriteLine("test dy load assembly");
}
}
}
using System.Reflection;
namespace Use_dy_Load_Assembly
{
class LoadExe
{
[STAThread]
static void Main(string[] args)
{
// Use the file name to load the assembly into the current application domain.
Assembly a = Assembly.LoadFrom("dy_loadAsse.exe");
Type [] types2 = a.GetTypes();
foreach (Type t in types2)
{
Console.WriteLine (t.FullName);
}
//Get the type to use.
//Type myType = a.GetType("OutUse"); 這樣寫老是出錯
Type myType = a.GetType("dy_loadAsse.OutUse");
Console.WriteLine (myType.FullName);
//Get the method to call.
MethodInfo mymethod = myType.GetMethod("Output");
// //Create an instance.
Object obj = Activator.CreateInstance(myType);
// //Execute the adnamemethod method.
mymethod.Invoke(obj,null);
//執行結果爲test dy load assembly
//以下是調用dy_loadAsse.exe中的Main方法,出現錯誤
// Type myType = a.GetType("dy_loadAsse.testclass");
// Console.WriteLine (myType.FullName);
// //Get the method to call.
// MethodInfo mymethod = myType.GetMethod("Main");
// //// //Create an instance.
// Object obj = Activator.CreateInstance(myType);
// //// //Execute the adnamemethod method.
// mymethod.Invoke(obj,null);
Console.ReadLine();
}
}
}
namespace dy_loaddll
{
public class HelloWorld
{
// Constant Hello World string.
private const String m_helloWorld = "Hello World";
// Default public constructor.
public HelloWorld()
{
}
// Print "Hello World" plus the passed text.
public void PrintHello(String txt)
{
// Output to the Console.
Console.WriteLine(m_helloWorld + " " + txt);
}
}
}
編輯生成dy_loaddll.dll,與 LoadExe.exe在同一的目錄下面。
using System;
using System.Reflection;
namespace Use_dy_Load_Assembly
{
class LoadExe
{
[STAThread]
static void Main(string[] args)
{
// Use the file name to load the assembly into the current application domain.
Assembly a = Assembly.LoadFrom("dy_loaddll.dll");
Type [] types2 = a.GetTypes();
foreach (Type t in types2)
{
Console.WriteLine (t.FullName);
}
//Get the type to use.
// Type myType = a.GetType(""); 這樣寫老是出錯因爲上面的dll加了命名空間
Type myType = a.GetType("dy_loaddll.HelloWorld");
// Type myType = a.GetType("HelloWorld");
Console.WriteLine (myType.FullName);
// //Get the method to call.
MethodInfo printMethod = myType.GetMethod("PrintHello");
// Create an instance of the HelloWorld class.
Object obj = Activator.CreateInstance(myType);
// Create the args array.
Object[] test = new Object[1];
// Set the arguments.
test[0] = "From CSharp Late Bound";
// Invoke the PrintHello method.
printMethod.Invoke(obj, test);
Console.ReadLine();
}
}
}
當然我們不禁要問題,如果方法重載,如果是屬性。又該怎麼辦??我們先看看一般的反射的動態方法查找
下面爲ms自帶的例子:
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
Console.WriteLine (" Reflection.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(" First method - " + MyTypea.FullName +" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write(" Second 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 。
namespace ClassLib1
{
public class Class1: IPoint
{
public Class1()
{
}
public string ReturnNowClass()
{
return "weclone Execute ClassLib1 Class1";
}
}
}
將ClassSuc.dll的也添加到上面文件,Class1實現了Ipoint接口編輯文件爲ClassLib1.dll
namespace ClassLib1
{
public class Class1: IPoint
{
public Class1()
{
}
public string ReturnNowClass()
{
return "weclone Execute ClassLib1 Class1";
}
}
}
將ClassSuc.dll的也添加到上面文件,Class1實現了Ipoint接口編輯文件爲ClassLib1.dll
namespace ClassLib2
{
public class Class2:IPoint
{
public Class2()
{
}
public string ReturnNowClass()
{
return "ClassLib2"+"Class2"+"weclone";
}
}
}
也許你已經看和做的不厭煩了,你可能要要問,你到底想講什麼????注意,將上面的三個dll copy 在 同一路徑下 這裏爲“C:/test”
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) 文件 (不同於上面的那個動態加載程序集)
在運行時定義程序集,然後運行這些程序集並/或將它們保存到磁盤。
在運行時定義新程序集中的模塊,然後運行這些模塊並/或將它們保存到磁盤。
在運行時定義類型,創建這些類型的實例,並調用這些類型的方法。
程序集---》 模塊---》類型—》實例的方法