当前位置:
首页 > 编程开发 > Objective-C编程 >
-
.net的reflection (2)
飞鹰编译 | ||
一旦得到类对象,上表中所列的方法就能被叫来调用reflaction.第一个例子将检查在CSharpReflectionSamples.Reflect类中的得到方法的信息。第一块代码用来定义类中的每个方法的名字,第二块代码将阐述得到方法信息。向下面所展示的,我们将用一个数组来保存用GetMethod()方法返回的方法信息。MethodInfo类包含信息为方法的名字,不管是否是虚拟的,它都是可见的,等等。 namespace CSharpReflectionSamples { using System; using System.Reflection; /// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType(); Console.WriteLine("Type of objTest is {0}", type2); Console.WriteLine(); // pause Console.ReadLine(); // reflect method information MethodInfo[] minfo = type1.GetMethods(); // iterate through methods foreach (MethodInfo m in minfo) { Console.WriteLine(m); } Console.WriteLine(); } } } 下一个例子将展示动态得到对象有可能接触的每个构造器的信息。类似与上面的例子,我们将返回一个包含每个构造器的信息ConstructorInfo对象。 namespace CSharpReflectionSamples { using System; using System.Reflection; /// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType(); Console.WriteLine("Type of objTest is {0}", type2); Console.WriteLine(); // pause Console.ReadLine(); // reflect constructors ConstructorInfo[] cinfo = type1.GetConstructors(); // iterate through constructors foreach (ConstructorInfo c in cinfo) { Console.WriteLine(c); } } } } 最后一部分,也许是reflection名字空间中最激动人心的部分,是在运行时动态调用类方法。有两种方法,首先,我们将建立一个数组来存储参数,这些参数被构造器用来建造对象。第二,一个System.Object对象将对抗CreateInstance方法的对象。以得到想得到对象的例子。最后,当我们有了对象的资料,我们能够调用任何使用MethodParm数组的方法。下面是代码: namespace CSharpReflectionSamples { using System; using System.Reflection; /// <summary> /// Summary description for Client. /// </summary> public class Client { public static void Main() { // the typeof operator and the GetType method // both return a 'Type' object. Type type1 = typeof(Reflect); Reflect objTest = new Reflect(0); Type type2 = objTest.GetType(); // dynamic creation and invocation // instantiate the Reflect object, passing // a value of 1 to the constructor object[] oConstructParms = new object[] {1}; object obj = Activator.CreateInstance(type1, oConstructParms); // invoke method of reflect object object[] oMethodParms = new object[] {17}; int intResult = (int)type1.InvokeMember("AMethod", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, oMethodParms); Console.WriteLine("Result of calling AMethod on {0} is {1}", type1.Name, intResult); // pause Console.ReadLine(); } } } 这篇文章阐述了.net Reflaction的基础,在下一部分,我将和大家讨论进一步的话题,比如,动态发布中间语言,旗帜绑定,和中间语言原则。 |
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程