自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#反射概念以及實例詳解

開發(fā) 后端
C#反射概念以及實例向你介紹了C#反射的基本內(nèi)容以及C#反射實例的簡單應(yīng)用,希望對你了解和學(xué)習(xí)C#反射以及C#反射實例有所幫助。

C#反射的入門學(xué)習(xí)首先要明白C#反射提供了封裝程序集、模塊和類型的對象等等。那么這樣可以使用反射動態(tài)創(chuàng)建類型的實例,將類型綁定到現(xiàn)有對象,或從現(xiàn)有對象獲取類型并調(diào)用其方法或訪問其字段和屬性。如果代碼中使用了屬性,可以利用反射對它們進行訪問。
 
一個最簡單的C#反射實例,首先編寫類庫如下: 

  1. using System;  
  2.  
  3. namespace ReflectionTest  
  4. {  
  5. public class WriteTest  
  6. {  
  7. //public method with parametors  
  8. public void WriteString(string s, int i)  
  9. {  
  10. Console.WriteLine("WriteString:" + s + i.ToString());  
  11. }  
  12.  
  13. //static method with only one parametor  
  14. public static void StaticWriteString(string s)  
  15. {  
  16. Console.WriteLine("StaticWriteString:" + s);  
  17. }  
  18.  
  19. //static method with no parametor  
  20. public static void NoneParaWriteString()  
  21. {  
  22. Console.WriteLine("NoParaWriteString");  
  23. }  
  24. }  

使用命令行編譯csc /t:library ReflectTest.cs命令進行編譯,生成ReflectTest.dll庫文件。
 
然后進行下列程序的編寫:

  1. using System;  
  2. using System.Reflection;  
  3.  
  4. class TestApp  
  5. {  
  6. public static void Main()  
  7. {  
  8. Assembly ass;  
  9. Type type;  
  10. Object obj;  
  11.  
  12. //Used to test the static method  
  13. Object any = new Object();  
  14.  
  15. //Load the dll  
  16. //Must indicates the whole path of dll  
  17. ass = Assembly.LoadFile(@"D:\Source Code\00.C#   
  18. Sudy\01.Reflection\01\ReflectTest.dll");   
  19. //Must be Namespace with class name  
  20. type = ass.GetType("ReflectionTest.WriteTest");  
  21.  
  22. /**//*example1---------*/ 
  23.  
  24. MethodInfo method = type.GetMethod("WriteString");  
  25.  
  26. string test = "test";  
  27. int i = 1;  
  28.  
  29. Object[] parametors = new Object[]{test,i};  
  30.  
  31. //Since the WriteTest Class is not Static you should Create the instance of this class  
  32. obj = ass.CreateInstance("ReflectionTest.WriteTest");  
  33.  
  34. method.Invoke(  
  35. obj,//Instance object of the class need to be reflect  
  36. parametors);//Parametors of indicated method  
  37.  
  38. //method.Invoke(any, parametors);//RuntimeError: class reference is wrong  
  39.  
  40. /**//*example2----------*/ 
  41.    
  42. method = type.GetMethod("StaticWriteString");  
  43.  
  44. //The first parametor will be ignored  
  45. method.Invoke(nullnew string[] { "test"});  
  46. method.Invoke(obj, new string[] { "test"});//indicates the instance will equals above line  
  47. method.Invoke(any, new string[] { "test"});//Even the class reference is wrong  
  48.  
  49. /**//*example3-----------*/ 
  50.  
  51. method = type.GetMethod("NoneParaWriteString");  
  52.  
  53. //Sine the method NoneParaWriteString()   
  54.  
  55. has no parametors so do not indicate any parametors  
  56. method.Invoke(nullnull);  
  57.  
  58. }  
  59. }  

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就可以了。

  1. output:  
  2. WriteString:test1  
  3. StaticWriteString:test  
  4. StaticWriteString:test  
  5. StaticWriteString:test  
  6. 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)用有所幫助。

【編輯推薦】

  1. 淺析什么是C#靜態(tài)方法
  2. C#靜態(tài)方法使用經(jīng)驗淺談
  3. C#靜態(tài)方法概念解析實例
  4. C#靜態(tài)方法與非靜態(tài)方法的比較
  5. C#靜態(tài)方法應(yīng)用實例詳解
責(zé)任編輯:仲衡 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2009-08-31 09:41:05

C#反射靜態(tài)方法開發(fā)

2021-03-15 08:18:23

C#反射模塊

2024-03-04 18:49:59

反射C#開發(fā)

2024-09-18 00:00:02

反射C#元數(shù)據(jù)

2009-08-28 12:31:06

C#靜態(tài)方法

2009-08-27 17:11:44

C# Fluent I

2009-08-31 16:23:13

C#接口

2009-08-20 11:01:51

C#操作內(nèi)存

2009-09-11 12:31:52

C#實例詳解TypeConvert

2009-08-18 10:14:19

C#插件構(gòu)架

2009-09-02 17:12:06

C#關(guān)機代碼

2009-09-09 10:47:29

C# CheckBox

2009-09-04 18:09:12

C# Main函數(shù)

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-09-02 19:12:37

C#遞歸

2009-09-07 05:50:59

C# Timer用法

2009-08-26 11:32:37

C#打印文檔

2009-09-01 11:25:08

C#讀取Word文件
點贊
收藏

51CTO技術(shù)棧公眾號