快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

.net的reflection (2)

飞鹰编译,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的基础,在下一部分,我将和大家讨论进一步的话题,比如,动态发布中间语言,旗帜绑定,和中间语言原则。


返回类别: 教程
上一教程: ASP.NET创建XML Web服务全接触(9)
下一教程: Win中Net命令的另类用法

您可以阅读与".net的reflection (2)"相关的教程:
· .net的reflection (1)
· Metadata and Reflection in .NET
· Visual Studio.NET企业级结构设计版评测
· 转贴自JoyASP:.NET框架(二)
· .NET 窗体之间的交互
    微笑服务 优质保证 索取样品