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

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

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

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

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

 
 
 
  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命令進(jìn)行編譯,生成ReflectTest.dll庫(kù)文件。
 
然后進(jìn)行下列程序的編寫:

 
 
 
  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#反射學(xué)習(xí)時(shí)幾點(diǎn)注意內(nèi)容:

1.指定類庫(kù)文件必須使用絕對(duì)路徑,不能使用相對(duì)路徑(其實(shí)感覺有點(diǎn)不合理,不太方便)

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

3.在例子1種必須實(shí)例化反射要反射的類,因?yàn)橐褂玫姆椒ú⒉皇庆o態(tài)方法。

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

5.在例子2種我們想用的方法是一個(gè)靜態(tài)方法,這時(shí)候Invoke的時(shí)候,對(duì)于第一個(gè)參數(shù)是無(wú)視的,也就是我們寫什么都不會(huì)被調(diào)用,即使我們隨便new了一個(gè)any這樣的Object,當(dāng)然這種寫法是不推薦的。但是對(duì)應(yīng)在例子1種我們?nèi)绻鸌nvoke的時(shí)候用了類型不一致的實(shí)例來(lái)做為參數(shù)的話,將會(huì)導(dǎo)致一個(gè)運(yùn)行時(shí)的錯(cuò)誤。

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

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

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

C#反射以及C#反射實(shí)例的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#反射以及C#反射實(shí)例應(yīng)用有所幫助。


分享標(biāo)題:C#反射概念以及實(shí)例詳解
標(biāo)題路徑:http://m.5511xx.com/article/dpihecj.html