Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
反射,Reflection,通过它我们可以在运行时获得各种信息,如程序集、模块、类型、字段、属性、方法和事件 通过对类型动态实例化后,还可以对其执行操作 
一般用于插件式框架程序和设计模式的实现,当然反射是一种手段可以充分发挥其能量来完成你想做的任何事情(前面好象见过一位高人用反射调用一个官方类库中未说明的函数。。。) 

示例: 
Code
反射实例化对象并调用其方法,属性和事件的反射调用略去 

using System; 
using System.Collections.Generic; 
using System.Text; 

//注意添加该反射的命名空间 
using System.Reflection; 

namespace Example25 

     
class Program 
     { 
         
static void Main(string[] args) 
         { 
             
//加载程序集 
             Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll"); 

             
//遍历程序集内所有的类型,并实例化 
             Type[] tmpTypes = tmpAss.GetTypes(); 
             
foreach (Type tmpType in tmpTypes) 
             { 
                 
//获取第一个类型的构造函数信息 
                 ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors(); 
                 
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos) 
                 { 
                     
//为构造函数生成调用的参数集合 
                     ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters(); 
                     
object[] tmpParams = new object[tmpParamInfos.Length]; 
                     
for (int i = 0; i < tmpParamInfos.Length; i++
                     { 
                         tmpParams[i] 
= tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName); 
                         
if (tmpParamInfos[i].ParameterType.FullName == "System.String"
                         { 
                             tmpParams[i] 
= "Clark"
                         } 
                     } 

                     
//实例化对象 
                     object tmpObj = tmpConsInfo.Invoke(tmpParams); 
                     Console.WriteLine(tmpObj); 

                     
//获取所有方法并执行 
                     foreach (MethodInfo tmpMethod in tmpType.GetMethods()) 
                     { 
                         
//为方法的调用创建参数集合 
                         tmpParamInfos = tmpMethod.GetParameters(); 
                         tmpParams 
= new object[tmpParamInfos.Length]; 
                         
for (int i = 0; i < tmpParamInfos.Length; i++
                         { 
                             tmpParams[i] 
= tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName); 
                             
if (tmpParamInfos[i].ParameterType.FullName == "System.String"
                             { 
                                 tmpParams[i] 
= "Clark Zheng"
                             } 
                             
if (tmpParamInfos[i].ParameterType.FullName == "System.Int32"
                             { 
                                 tmpParams[i] 
= 27
                             } 
                         } 
                         tmpMethod.Invoke(tmpObj, tmpParams); 
                     } 

                     
//调用完方法后再次打印对象,比较结果 
                     Console.WriteLine(tmpObj); 
                 } 
             } 

             Console.ReadLine(); 
         } 
     } 
结果: 
Name: Clark, Age: 0 

Name: Clark Zheng, Age: 27 


----

其中Object 对其调用方法或构造函数的对象。如果方法是静态的,则忽略此参数。如果构造函数是静态的,则此参数必须为 null 引用(在 Visual Basic 中为 Nothing) 或定义该构造函数的类的实例。 

MethodBase.Invoke 方法 (Object, Object[])


posted on 2009-03-16 13:45  Robin99  阅读(279)  评论(0)    收藏  举报