C#反射查看和调用程序集的类和方法.doc_第1页
C#反射查看和调用程序集的类和方法.doc_第2页
C#反射查看和调用程序集的类和方法.doc_第3页
C#反射查看和调用程序集的类和方法.doc_第4页
C#反射查看和调用程序集的类和方法.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#反射查看程序集中的类和方法说明:1.在vs2005中,新建控制台项目:TestReflection2.右键“解决方案”,点击“添加”“新建项目”,选择“类库”,将类库命名为TestClassLibrary3.将类库TestClassLibrary的生成-输出路径设置到TestReflection的bindebug目录下。解决方案下的文件如截图:4.在类库TestClassLibrary项目中,删除默认的类Class1,添加3个类,分别是:OperatorClass,Test1,Test2。这三个类的代码如下:类OperatorClass源代码:using System;using System.Collections.Generic;using System.Text;namespace TestClassLibrary / / 基本运算操作类 / public class OperatorClass / / 加法 / / / / public static int Add(int x, int y) return x + y; / / 减法 / / / / public static int Subtract(int x, int y) return x - y; / / 乘法 / / / / public int Mulitiple(int x, int y) return x * y; / / 除法 / / / / public int Divide(int x, int y) if (y = 0) Console.WriteLine(除法操作时,除数不能为,返回值暂设定为.); return 0; return x / y; / / 取余数求余 / / / / public int Remain(int x, int y) if (y = 0) Console.WriteLine(取余操作时,除数不能为,返回值暂设定为.); return 0; return x % y; / / 获取数组的最大数以及最大数所在的索引 / / 数组 / 输出最大值的所在的索引 / public int GetMaxNumber(int array, out int maxIndex) if (array = null | array.Length = 0) Console.WriteLine(数组不能为null,数组的长度也不能为,暂定返回值为-1); maxIndex = -1; return -1; int max = array0; maxIndex = 0; for (int i = 1; i array.Length; i+) if (max arrayi) maxIndex = i; max = arrayi; return max; / / 获得不定项数组的长度 / / / public int GetVaryArrayLength(params int array) if (array = null) return 0; return array.Length; / / 测试带ref的参数 / / / public void TestReference(int inParameter, ref int refParameter) inParameter = 5; refParameter = 2; Console.WriteLine(参数默认为输入参数,如:inParameter,不会改变原来的值.rn带ref或者out的参数是引用参数,如:refParameter,可以改变原来的值.); / / 私有方法无法通过反射得到该方法的信息 / private void PrivateMethod() Console.WriteLine(私有方法); 类Test1源代码:using System;using System.Collections.Generic;using System.Text;namespace TestClassLibrary class Test1 public static void Func1() Console.WriteLine(Func1); public static void Func2() Console.WriteLine(Func2); 类Test2源代码:using System;using System.Collections.Generic;using System.Text;namespace TestClassLibrary class Test2 public void Method1() Console.WriteLine(Method1); public void Method2() Console.WriteLine(Method2); 切换到控制台TestReflection,类Program源代码如下:using System;using System.Collections.Generic;using System.Text;using System.Reflection;namespace TestReflection class Program static void Main(string args) Assembly assembly = Assembly.Load(TestClassLibrary); Console.WriteLine(程序集信息:n + assembly.FullName); Console.WriteLine(); Type ts = assembly.GetTypes();/获得程序集中定义的类型 for (int i = 0; i ts.Length; i+) string typeFullName = tsi.FullName; Console.WriteLine(类型0:1的所有公共方法:, i + 1, typeFullName); MethodInfo mis = tsi.GetMethods();/返回当前类型的所有公共方法 for (int j = 0; j mis.Length; j+) Console.WriteLine(方法名0:1,返回值的类型:2,是静态方法:3, j + 1, misj.Name, misj.ReturnType, misj.IsStatic); ParameterInfo paras = misj.GetParameters();/获取指定的方法或构造函数的参数信息 for (int k = 0; k paras.Length; k+) /当我们知道方法的类型是引用类型时(带有&表示引用类型) 如:System.Int32&,此时参数带ref或out修饰 /如果参数是输出参数(parask.IsOut=true),则参数的修饰是out,否则是ref Console.WriteLine( 参数0:1 2,签名位置:3,是输出参数:4, k + 1, parask.ParameterType, parask.Name, parask.Position, parask.IsOut); Console.WriteLine(); Console.WriteLine(当我们知道方法的名称,返回值信息和参数信息时,就可以反射调用该方法或构造函数了.); TestInvokeMethod(); Console.ReadLine(); / / 测试调用类库中的方法 / static void TestInvokeMethod() Type ty = System.Reflection.Assembly.Load(TestClassLibrary).GetType(string.Format(0.1, TestClassLibrary, OperatorClass); Console.WriteLine(调用TestClassLibrary.OperatorClass 类的公共静态方法public static int Add(int x, int y) 的例子); System.Reflection.MethodInfo mi = ty.GetMethod(Add); /调用静态方法Add时,Invoke的第一个参数为null object returnValue = mi.Invoke(null, new object 2, 3 ); Console.WriteLine(Add方法的返回值:0rn, returnValue); Console.WriteLine(调用TestClassLibrary.OperatorClass 类的公共方法public int GetMaxNumber(int array, out int maxIndex) 的例子); System.Reflection.MethodInfo mi1 = ty.GetMethod(GetMaxNumber); /调用实例化方法(非静态方法)需要创建类型的一个实例 object instanceObject = Activator.CreateInstance(ty); int maxIndex = -1; object parametersInfo = new object new int 8, 20, 15, 36, 1, 2 , maxIndex ; /调用实例化方法GetMaxNumber时,Invoke的第一个参数

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论