反射使用的简单实例
2007-08-23 15:45
579 查看
//创建一个程序集实例
System.Reflection.Assembly a= System.Reflection.Assembly.LoadFrom(Server.MapPath("../Public/bin/Debug/Public.dll"));
//定义一个类型数组并从程序集实例中获得
Type[] t = a.GetTypes();
//循环输出类型数组中的类的名称
for(int i = 0 ; i<t.Length;i++)
{
Response.Write(t[i].Name+"<br>");
}
//定义一个方法信息实例 并从程序集中指定名称的类中指定名称的方法获得
System.Reflection.MethodInfo m = a.GetType("Public.Jscript").GetMethod("Alert");
//同上,只不过是通过索引获得 t是a.GetTypes数组
//System.Reflection.MethodInfo m = t[3].GetMethod("Alert");
//调用方法信息实例的Invoke方法,并传递参数,来执行反射做要指定指定的类的方法,其中参数1是类创建的实例,参数2是所要执行的方法的参数的对象数组 *说明:System.Activator.CreateInstance是动态创建指定类实例的方法
Response.Write(m.Invoke(System.Activator.CreateInstance(a.GetType("Public.Jscript")),new object[]{"欢迎!"}));
System.Reflection.Assembly a= System.Reflection.Assembly.LoadFrom(Server.MapPath("../Public/bin/Debug/Public.dll"));
//定义一个类型数组并从程序集实例中获得
Type[] t = a.GetTypes();
//循环输出类型数组中的类的名称
for(int i = 0 ; i<t.Length;i++)
{
Response.Write(t[i].Name+"<br>");
}
//定义一个方法信息实例 并从程序集中指定名称的类中指定名称的方法获得
System.Reflection.MethodInfo m = a.GetType("Public.Jscript").GetMethod("Alert");
//同上,只不过是通过索引获得 t是a.GetTypes数组
//System.Reflection.MethodInfo m = t[3].GetMethod("Alert");
//调用方法信息实例的Invoke方法,并传递参数,来执行反射做要指定指定的类的方法,其中参数1是类创建的实例,参数2是所要执行的方法的参数的对象数组 *说明:System.Activator.CreateInstance是动态创建指定类实例的方法
Response.Write(m.Invoke(System.Activator.CreateInstance(a.GetType("Public.Jscript")),new object[]{"欢迎!"}));
相关文章推荐
- 抽象工厂模式:简单工厂模式、工厂方法模式对比;在工厂方法模式中使用反射创建对象实例
- 反射使用的简单实例
- .net中使用反射的简单例子
- windows下使用RedisCluster集群简单实例
- tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
- jQuery easyui combobox的使用 简单实例
- javascript使用xml 数据岛 简单实例
- Android Service使用方法--简单音乐播放实例
- 一个使用Ruby on Rails开发LBS网站的简单实例
- android简单实例---------ActionBar的简单使用(三,补充)
- Android中SharedPreferences简单使用实例
- C# 中SharpMap的简单使用实例详解
- delphi下使用indy进件socket通信的简单实例介绍
- Java反射的简单使用
- deviceDriver.py与robot framework结合使用的简单实例
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
- log4donet 的 一篇简单使用实例
- java 使用线程做一个简单的ATM存取款实例.(转)
- android中反射技术使用实例
- libevent的使用方法--回显服务器的简单实例