日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#反射概念以及實例詳解

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

創(chuàng)新互聯(lián)網(wǎng)站建設公司是一家服務多年做網(wǎng)站建設策劃設計制作的公司,為廣大用戶提供了網(wǎng)站建設、成都網(wǎng)站制作,成都網(wǎng)站設計,廣告投放平臺,成都做網(wǎng)站選創(chuàng)新互聯(lián),貼合企業(yè)需求,高性價比,滿足客戶不同層次的需求一站式服務歡迎致電。

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

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

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

C#反射學習時幾點注意內容:

1.指定類庫文件必須使用絕對路徑,不能使用相對路徑(其實感覺有點不合理,不太方便)

2.19行,命名空間和類的名字必須一起指定

3.在例子1種必須實例化反射要反射的類,因為要使用的方法并不是靜態(tài)方法。

4.由于這個方法有兩個參數(shù),可以用這種Object的方法指定參數(shù)也可以直接寫method.Invoke(obj, new Object[] { "test", 1 });

5.在例子2種我們想用的方法是一個靜態(tài)方法,這時候Invoke的時候,對于第一個參數(shù)是無視的,也就是我們寫什么都不會被調用,即使我們隨便new了一個any這樣的Object,當然這種寫法是不推薦的。但是對應在例子1種我們如果Invoke的時候用了類型不一致的實例來做為參數(shù)的話,將會導致一個運行時的錯誤。

6.第三個例子是一個調用無參數(shù)靜態(tài)方法的例子,這時候兩個參數(shù)我們都不需要指定,用null就可以了。

 
 
 
  1. output:
  2. WriteString:test1
  3. StaticWriteString:test
  4. StaticWriteString:test
  5. StaticWriteString:test
  6. NoParaWriteString

再說一個問題,如果調用的類是靜態(tài)類的時候,需要注意一個問題,肯定我們會想到一個問題,靜態(tài)類是不能實例化的,這時候,31行的類的實例化的方法我們就不需要了,直接使用Invoke就可以實現(xiàn),否則將會出現(xiàn)運行時的錯誤,同樣的道理,第一個參數(shù)將會被無視,只要我們傳對了參數(shù)就可以了。

C#反射以及C#反射實例的相關內容就向你介紹到這里,希望對你了解和學習C#反射以及C#反射實例應用有所幫助。


網(wǎng)站欄目:C#反射概念以及實例詳解
文章轉載:http://m.5511xx.com/article/dpihecj.html