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

C#反射概念以及实例详解 http://developer.51cto.com 2009-08-28 13:12 佚名 互联网 我要评论(0) C#反射概念以及实例向你介绍了C#反射的基本内容以及C#反射实例的简单应用,希望对你了解和学习C#反射以及C#反射实例有所帮助。 C#反射的入门学习首先要明白C#反射提供了封装程序集、模块和类型的对象等等。那么这样可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。 一个最简单的C#反射实例,首先编写类库如下: using System; namespace ReflectionTest { public class WriteTest { //public method with parametors public void WriteString(string s, int i) { Console.WriteLine("WriteString:" + s + i.ToString()); } //static method with only one parametor public static void StaticWriteString(string s) { Console.WriteLine("StaticWriteString:" + s); } //static method with no parametor public static void NoneParaWriteString() { Console.WriteLine("NoParaWriteString"); } } } 使用命令行编译csc /t:library ReflectTest.cs命令进行编译,生成ReflectTest.dll库文件。 然后进行下列程序的编写: using System; using System.Reflection; class TestApp { public static void Main() { Assembly ass; Type type; Object obj; //Used to test the static method Object any = new Object(); //Load the dll //Must indicates the whole path of dll ass = Assembly.LoadFile(@"D:/Source Code/00.C# Sudy/01.Reflection/01/ReflectTest.dll"); //Must be Namespace with class name type = ass.GetType("ReflectionTest.WriteTest"); /**//*example1---------*/ MethodInfo method = type.GetMethod("WriteString"); string test = "test"; int i = 1; Object[] parametors = new Object[]{test,i}; //Since the WriteTest Class is not Static you should Create the instance of this class obj = ass.CreateInstance("ReflectionTest.WriteTest"); method.Invoke( obj,//Instance object of the class need to be reflect parametors);//Parametors of indicated method //method.Invoke(any, parametors);//RuntimeError: class reference is wrong /**//*example2----------*/ method = type.GetMethod("StaticWriteString"); //The first parametor will be ignored method.Invoke(null, new string[] { "test"}); method.Invoke(obj, new string[] { "test"});//indicates the instance will equals above line method.Invoke(any, new string[] { "test"});//Even the class reference is wrong /**//*example3-----------*/ method = type.GetMethod("NoneParaWriteString"); //Sine the method NoneParaWriteString() has no parametors so do not indicate any parametors method.Invoke(null, null); } } 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就可以了。 output: WriteString:test1 StaticWriteString:test StaticWriteString:test StaticWriteString:test NoParaWriteString 再说一个问题,如果调用的类是静态类的时候,需要注意一个问题,肯定我们会想到一个问题,静态类是不能实例化的,这时候,31行的类的实例化的方法我们就不需要了,直接使用Invoke就可以实现,否则将会出现运行时的错误,同样的道理,第一个参数将会被无视,只要我们传对了参数就可以了。 C#反射以及C#反射实例的相关内容就向你介绍到这里,希望对你了解和学习C#反射以及C#反射实例应用有所帮助。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值