C# 反射实例

先讨论一个最简单的例子,首先编写类库如下:
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  ReflectionTest
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif    public class WriteTest
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        //public method with parametors
 8InBlock.gif        public void WriteString(string s, int i)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            Console.WriteLine("WriteString:" + s + i.ToString());
11ExpandedSubBlockEnd.gif        }

12InBlock.gif
13InBlock.gif        //static method with only one parametor
14InBlock.gif        public static void StaticWriteString(string s)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            Console.WriteLine("StaticWriteString:" + s);
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif        //static method with no parametor
20InBlock.gif        public static void NoneParaWriteString()
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            Console.WriteLine("NoParaWriteString");
23ExpandedSubBlockEnd.gif        }

24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}

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

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

56ExpandedBlockEnd.gif}
几点注意内容:
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就可以实现,否则将会出现运行时的错误,同样的道理,第一个参数将会被无视,只要我们传对了参数就可以了。


转载于:https://www.cnblogs.com/kevin8000903/archive/2007/05/23/757222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值