C# 反射实例

先讨论一个最简单的例子,首先编写类库如下:
 1using System;
 2
 3namespace 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    }

25}

使用命令行编译csc /t:library ReflectTest.cs命令进行编译,生成ReflectTest.dll库文件

然后进行应用程序的编写
 1using System;
 2using System.Reflection;
 3
 4class 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# Sudy\01.Reflection\01\ReflectTest.dll"); 
18        //Must be Namespace with class name
19        type = ass.GetType("ReflectionTest.WriteTest");
20
21        /*example1------------------------------------------------------------------------------------*/
22
23        MethodInfo method = type.GetMethod("WriteString");
24
25        string test = "test";
26        int i = 1;
27
28        Object[] parametors = new Object[]{test,i};
29
30        //Since the WriteTest Class is not Static you should Create the instance of this class
31        obj = ass.CreateInstance("ReflectionTest.WriteTest");
32
33        method.Invoke(
34            obj,            //Instance object of the class need to be reflect
35            parametors);    //Parametors of indicated method
36
37        //method.Invoke(any, parametors);//RuntimeError: class reference is wrong
38
39        /*example2------------------------------------------------------------------------------------*/
40                
41        method = type.GetMethod("StaticWriteString");
42
43        //The first parametor will be ignored
44        method.Invoke(nullnew string[] "test"});
45        method.Invoke(obj, new string[] "test"});//indicates the instance will equals above line
46        method.Invoke(any, new string[] "test"});//Even the class reference is wrong
47        
48        /*example3------------------------------------------------------------------------------------*/
49
50        method = type.GetMethod("NoneParaWriteString");
51
52        //Sine the method NoneParaWriteString() has no parametors so do not indicate any parametors
53        method.Invoke(nullnull);
54
55    }

56}
几点注意内容:
1.指定类库文件必须使用绝对路径,不能使用相对路径(其实感觉有点不合理,不太方便)
2.19行,命名空间和类的名字必须一起指定
3.在例子1种必须实例化反射要反射的类,因为要使用的方法并不是静态方法。
4.由于这个方法有两个参数,可以用这种Object的方法指定参数也可以直接写method.Invoke(obj, new Object[] { "test", 1 });
5.在例子2种我们想用的方法是一个静态方法,这时候Invoke的时候,对于第一个参数是无视的,也就是我们写什么都不会被调用,即使我们随便new了一个any这样的Object,当然这种写法是不推荐的。但是对应在例子1种我们如果Invoke的时候用了类型不一致的实例来做为参数的话,将会导致一个运行时的错误。
6.第三个例子是一个调用无参数静态方法的例子,这时候两个参数我们都不需要指定,用null就可以了。
output:
WriteString:test1
StaticWriteString:test
StaticWriteString:test
StaticWriteString:test
NoParaWriteString

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


posted on 2007-05-23 15:33  Big Angel  阅读(2530)  评论(1编辑  收藏  举报

导航