C#反射概念以及实例详解

开发 后端
C#反射概念以及实例向你介绍了C#反射的基本内容以及C#反射实例的简单应用,希望对你了解和学习C#反射以及C#反射实例有所帮助。

C#反射的入门学习首先要明白C#反射提供了封装程序集、模块和类型的对象等等。那么这样可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。
 
一个最简单的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#反射学习时几点注意内容:

1.指定类库文件必须使用绝对路径,不能使用相对路径(其实感觉有点不合理,不太方便)

2.19行,命名空间和类的名字必须一起指定

3.在例子1种必须实例化反射要反射的类,因为要使用的方法并不是静态方法。

4.由于这个方法有两个参数,可以用这种Object的方法指定参数也可以直接写method.Invoke(obj, new Object[] { "test", 1 });

5.在例子2种我们想用的方法是一个静态方法,这时候Invoke的时候,对于第一个参数是无视的,也就是我们写什么都不会被调用,即使我们随便new了一个any这样的Object,当然这种写法是不推荐的。但是对应在例子1种我们如果Invoke的时候用了类型不一致的实例来做为参数的话,将会导致一个运行时的错误。

6.第三个例子是一个调用无参数静态方法的例子,这时候两个参数我们都不需要指定,用null就可以了。

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

再说一个问题,如果调用的类是静态类的时候,需要注意一个问题,肯定我们会想到一个问题,静态类是不能实例化的,这时候,31行的类的实例化的方法我们就不需要了,直接使用Invoke就可以实现,否则将会出现运行时的错误,同样的道理,第一个参数将会被无视,只要我们传对了参数就可以了。

C#反射以及C#反射实例的相关内容就向你介绍到这里,希望对你了解和学习C#反射以及C#反射实例应用有所帮助。

【编辑推荐】

  1. 浅析什么是C#静态方法
  2. C#静态方法使用经验浅谈
  3. C#静态方法概念解析实例
  4. C#静态方法与非静态方法的比较
  5. C#静态方法应用实例详解
责任编辑:仲衡 来源: 互联网
相关推荐

2009-08-31 09:41:05

C#反射静态方法开发

2021-03-15 08:18:23

C#反射模块

2024-03-04 18:49:59

反射C#开发

2009-08-28 12:31:06

C#静态方法

2009-08-27 17:11:44

C# Fluent I

2009-08-31 16:23:13

C#接口

2009-09-02 17:12:06

C#关机代码

2009-08-20 11:01:51

C#操作内存

2009-08-18 10:14:19

C#插件构架

2009-09-11 12:31:52

C#实例详解TypeConvert

2009-09-09 10:47:29

C# CheckBox

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 12:47:30

C#静态方法应用

2009-09-01 11:25:08

C#读取Word文件

2009-08-21 10:13:02

C#异步初步

2009-08-26 11:32:37

C#打印文档

2009-09-04 18:09:12

C# Main函数

2009-09-07 05:50:59

C# Timer用法

2009-08-26 11:07:36

C#打印窗体

2009-09-02 19:12:37

C#递归
点赞
收藏

51CTO技术栈公众号