C#反射概念以及實例詳解
C#反射的入門學(xué)習(xí)首先要明白C#反射提供了封裝程序集、模塊和類型的對象等等。那么這樣可以使用反射動態(tài)創(chuàng)建類型的實例,將類型綁定到現(xiàn)有對象,或從現(xiàn)有對象獲取類型并調(diào)用其方法或訪問其字段和屬性。如果代碼中使用了屬性,可以利用反射對它們進行訪問。
一個最簡單的C#反射實例,首先編寫類庫如下:
- using System;
- 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.Reflection;
- class TestApp
- {
- public static void Main()
- {
- Assembly ass;
- Type type;
- Object obj;
- //Used to test the static method
- Object any = new Object();
- //Load the dll
- //Must indicates the whole path of dll
- ass = Assembly.LoadFile(@"D:\Source Code\00.C#
- Sudy\01.Reflection\01\ReflectTest.dll");
- //Must be Namespace with class name
- type = ass.GetType("ReflectionTest.WriteTest");
- /**//*example1---------*/
- MethodInfo method = type.GetMethod("WriteString");
- string test = "test";
- int i = 1;
- Object[] parametors = new Object[]{test,i};
- //Since the WriteTest Class is not Static you should Create the instance of this class
- obj = ass.CreateInstance("ReflectionTest.WriteTest");
- method.Invoke(
- obj,//Instance object of the class need to be reflect
- parametors);//Parametors of indicated method
- //method.Invoke(any, parametors);//RuntimeError: class reference is wrong
- /**//*example2----------*/
- 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"});//Even the class reference is wrong
- /**//*example3-----------*/
- method = type.GetMethod("NoneParaWriteString");
- //Sine the method NoneParaWriteString()
- has no parametors so do not indicate any parametors
- method.Invoke(null, null);
- }
- }
C#反射學(xué)習(xí)時幾點注意內(nèi)容:
1.指定類庫文件必須使用絕對路徑,不能使用相對路徑(其實感覺有點不合理,不太方便)
2.19行,命名空間和類的名字必須一起指定
3.在例子1種必須實例化反射要反射的類,因為要使用的方法并不是靜態(tài)方法。
4.由于這個方法有兩個參數(shù),可以用這種Object的方法指定參數(shù)也可以直接寫method.Invoke(obj, new Object[] { "test", 1 });
5.在例子2種我們想用的方法是一個靜態(tài)方法,這時候Invoke的時候,對于第一個參數(shù)是無視的,也就是我們寫什么都不會被調(diào)用,即使我們隨便new了一個any這樣的Object,當(dāng)然這種寫法是不推薦的。但是對應(yīng)在例子1種我們?nèi)绻鸌nvoke的時候用了類型不一致的實例來做為參數(shù)的話,將會導(dǎo)致一個運行時的錯誤。
6.第三個例子是一個調(diào)用無參數(shù)靜態(tài)方法的例子,這時候兩個參數(shù)我們都不需要指定,用null就可以了。
- output:
- WriteString:test1
- StaticWriteString:test
- StaticWriteString:test
- StaticWriteString:test
- NoParaWriteString
再說一個問題,如果調(diào)用的類是靜態(tài)類的時候,需要注意一個問題,肯定我們會想到一個問題,靜態(tài)類是不能實例化的,這時候,31行的類的實例化的方法我們就不需要了,直接使用Invoke就可以實現(xiàn),否則將會出現(xiàn)運行時的錯誤,同樣的道理,第一個參數(shù)將會被無視,只要我們傳對了參數(shù)就可以了。
C#反射以及C#反射實例的相關(guān)內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#反射以及C#反射實例應(yīng)用有所幫助。
【編輯推薦】