网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
.net的reflection (2)
[ 来源: | 作者: | 时间:2004-6-28 12:18:00 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  
飞鹰编译,www.aspcool.com版权所有,转载时请保留此信息。

一旦得到类对象,上表中所列的方法就能被叫来调用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的基础,在下一部分,我将和大家讨论进一步的话题,比如,动态发布中间语言,旗帜绑定,和中间语言原则。


[发送给好友]  [打印本页]  [关闭窗口]  [返回顶部]   转载请注明来源:http://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: chinaz.com
原始作者: 录入时间: 2004-6-28 12:18:00
信息来源: 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿