http://blog.sina.com.cn/s/blog_3e6cb7680100lg1h.html
C#反射的入門學習首先要明白C#反射提供了封裝程式集、模組和類型的對象等等。那麼這樣可以使用反射動態建立類型的執行個體,將類型綁定到現有對象,或從現有對象擷取類型並調用其方法或訪問其欄位和屬性。如果代碼中使用了屬性,可以利用反射對它們進行訪問。
一個最簡單的C#反射執行個體,首先編寫類庫如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ReflectionTest
{
public class WriteTest
{
//public method with parametors
public void WriteString(string s, int i)
{
Console.WriteLine("WriteString:" s i.ToString());
}
//static method with only one parametor
public static void StaticWriteString(string s)
{
Console.WriteLine("StaticWriteString:" s);
}
//static method with no parametor
public static void NoneParaWriteString()
{
Console.WriteLine("NoParaWriteString");
}
}
}
使用命令列編譯csc /t:library ReflectTest.cs命令進行編譯,產生ReflectTest.dll庫檔案。
然後進行下列程式的編寫:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ReflectionTest
{
class Program
{
static void Main(string[] args)
{
Assembly ass;
Type type;
Object obj;
Object any = new Object();
// 擷取程式集ass
ass = Assembly.LoadFile(@"D:\Documents and Settings\peterming\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ReflectionDll\bin\Debug\ReflectionDll.dll");
// 擷取程式集ass中的一個類型
type = ass.GetType("ReflectionTest.WriteTest");
// 擷取類型type的一個方法method
MethodInfo method = type.GetMethod("WriteString");
// 建立兩個參數的數組
string test = "test";
int i = 1;
Object[] parametors = new Object[] { test, i };
// 建立程式集ass中的type.FullName類的一個對象
obj = ass.CreateInstance(type.FullName);
// 方法method, 對象obj,參數parametors
method.Invoke(obj, parametors);
//Parametors of indicated method
//method.Invoke(any, parametors);//RuntimeError: class reference is wrong
method = type.GetMethod("StaticWriteString");
//The first parametor will be ignored
method.Invoke(null, new string[] { "test" });
method.Invoke(obj, new string[] { "test" });
//indicates the instance will equals above line
method.Invoke(any, new string[] { "test" });
method = type.GetMethod("NoneParaWriteString");
//Sine the method NoneParaWriteString() has no parametors so do not indicate any parametors
method.Invoke(null, null);
Console.ReadLine();
}
}
}
C#反射學習時幾點注意內容:
1.指定類庫檔案必須使用絕對路徑,不能使用相對路徑(其實感覺有點不合理,不太方便)
2.19行,命名空間和類的名字必須一起指定
3.在例子1種必須執行個體化反射要反射的類,因為要使用的方法並不是靜態方法。
4.由於這個方法有兩個參數,可以用這種Object的方法指定參數也可以直接寫method.Invoke(obj, new Object[] { "test", 1 });
5.在例子2種我們想用的方法是一個靜態方法,這時候Invoke的時候,對於第一個參數是無視的,也就是我們寫什麼都不會被調用,即使我們隨便new了一個any這樣的Object,當然這種寫法是不推薦的。但是對應在例子1種我們如果Invoke的時候用了類型不一致的執行個體來做為參數的話,將會導致一個運行時的錯誤。
6.第三個例子是一個調用無參數靜態方法的例子,這時候兩個參數我們都不需要指定,用null就可以了。
- output:
- WriteString:test1
- StaticWriteString:test
- StaticWriteString:test
- StaticWriteString:test
- NoParaWriteString
再說一個問題,如果調用的類是靜態類的時候,需要注意一個問題,肯定我們會想到一個問題,靜態類是不能執行個體化的,這時候,31行的類的執行個體化的方法我們就不需要了,直接使用Invoke就可以實現,否則將會出現運行時的錯誤,同樣的道理,第一個參數將會被無視,只要我們傳對了參數就可以了。
反射實現步驟總結:
1:通過動態連結程式庫的具體位置擷取程式集
Assembly ass = Assembly.LoadFile(@"D:\Documents and Settings\peterming\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ReflectionDll\bin\Debug\ReflectionDll.dll");
2:通過程式集Assembly擷取需要的類的類型Type type(程式集中可能含有多個類)
System.Type type = ass.GetType("ReflectionTest.WriteTest");// 參數是該類在程式集中的完整位置(命名空間 類名)
3:通過type來擷取類的函數(函數的屬性)
MethodInfo method = type.GetMethod("WriteString"); //參數是方法名
4:通過程式集對象初始化程式集中一個類的執行個體
Object obj = ass.CreateInstance(type.FullName); // 參數是該類在程式集中的位置(命名空間 類名)
5:建立函數執行需要的參數數組
// 建立兩個參數的數組
string test = "test";
int i = 1;
Object[] parametors = new Object[] { test, i };
6:執行該函數
method.Invoke(obj,parametors);
方法二:
string StrFileName = "d:\\1.rar"; //根據實際情況設定
string StrUrl = "http://localhost/MJGPS/Images/carIcon.rar"; //根據實際情況設定 中國電子調度系統\SQLPERSONAL\SQLPERSONAL\BOOKS
// 參數為程式集名稱
Assembly assembly = Assembly.Load("FileTransmit");
// 建立FileTransmitNameSpace.FileTransmitClass的對象
// FileTransmitNameSpace命名空間
// FileTransmitClass命名空間裡的類
FileTransmitNameSpace.FileTransmitClass transmit = (FileTransmitNameSpace.FileTransmitClass)assembly.CreateInstance("FileTransmitNameSpace.FileTransmitClass");
// 調用對象的方法
transmit.downloadFile(StrUrl, StrFileName);