当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > .net的reflection (2)

ASP.NET
从一个舆论调查的制作谈面向对象的编程思路(二)
从一个舆论调查的制作谈面向对象的编程思路(三)
从一个舆论调查的制作谈面向对象的编程思路(四)
从一个舆论调查的制作谈面向对象的编程思路(五)
c#写的五子棋程序,供学习WinForms的鼠标事件和使用GDI+
我对.Net技术中asp.net应用的一点看法
ASP.NET连SQL7接口源代码
ASP.NET中密码保护,MD5和SHA1算法的使用
微软的远程处理框架.NET Remoting - 1
微软的远程处理框架.NET Remoting - 2
黑客写出了针对.Net平台的反编译器
在config.web中保存数据库连接串
在ASP.Net中两种利用CSS实现多界面的方法
ASP.NET中如何调用存储过程
ASP.NET中取代ASP的RS(Remote Scripting)技术的Framework
刚学ASP.Net,学了个简单的计算器
ASP.NET 链接数据库基础
一个简单的加密/解密方法
加入身份验证信息的SMTP mail发送
webconfig的设置节点说明

ASP.NET 中的 .net的reflection (2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 31 ::
收藏到网摘: n/a

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