网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP文摘
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.用VB构建Internet的应用.
.关于如何保障Winnt +asp +sql we.
.从VB 6.0到VB.NET的转换(2).
.动态网页技术--CGI:ASP:JSP:PHP(.
.ASP+和JSP之间的抉择.
.从VB 6.0到VB.NET的转换(4).
.学以致用 驳“ASP低能论” .
.动态网页技术--CGI:ASP:JSP:PHP(.
.ASP.NET强大的性能(一).
.<展现C#> 第一章 C#简介.
.加固NT和IIS的安全.
..Net的精髓-XML和SOAP(一).
.IIS6.0下ASP的新增功能.
.动态网页制作(二).
.企业级N Tier体系结构解决方案讨.
..NET让新一代因特网变成现实(5).
.<展现C#> 第八章 用C#写组.
.技术分析:.NET的优势与劣势.
.重返ASP时代.
..NET让新一代因特网变成现实(1).

.net的reflection (1) 

发表日期:2001-4-18


在我的文章《C#基于接口的编程》中,我谈论了使用抽象的接口作为编程范例的各种优点。分离接口和执行过程,现在肯定不是新的思想,实际上它是com编程的核心。也许基于接口的在显著的特征是多态性和即时检查(RTTI).RTTI允许客户端程序在运行时访问对象。例如,如果一个对象执行IAthlete接口,一个客户端程序能够查找然后绑定这个接口用于调用和定位另一个接口。
查询接口是强大的,是com+的基础。同时,它对能够在一个对象上执行接口导向又是极端有用的和明确必要的,这是.net一个特别重要的概念。在介绍这个概念之前,让我们再回顾一些信息关于在.net中功能性和分布的基础元素--集合。
在COM(+),组件展开的逻辑和物理单元是一个.dll文件。在.net平台中,展开的基础单元是集合。与COM(+)组件不同的是一个.net或许是有多个文件组成,尽管它被看作是一个单一的何不可分割的功能和执行单元。一个集合文件也是自描述的,它包含被称之为中间语言的管制代码和附加的元数据,这个元数据给想绑定到这些代码的客户提供丰富的信息。.net的每个集合是著名的清单数据结构。清单包含下列信息:
1、姓名和版本信息
2、其它集合的参考信息
3、安全边界的信息。
4、集合中所有类型的信息

毫无疑问,集合清单类似于COM(+)的类型库。清单有在类型库中不存在的优点。这个超过了这一课的范围,我们将在这篇文章的第二部分进行讲述。被利用.net集合的客户端程序将要使用清单。因为每个集合包含元数据的许多信息,客户程序在运行时能够得到集合的内容。这个过程就叫做reflection.Reflection不仅能够查询基础类,而且运行时能动态调用方法,甚至于输出能够直接执行的IL代码。在你的代码中有两种方法来使用reflection,Reflection API 和 System.Reflection 名字空间。System.reflection是极端负责的,它包含将近40个类。在文章的其他部分,我将介绍reflection的基础,如何用它区查询集合类,方法,以及运行时调用方法的类型。为了示范在.net中的reflection名字空间,我将用下面的"reflection"类来说明:

// Reflect.cs
namespace CSharpReflectionSamples
{
using System;

/// <summary>
/// The reflect class will be used to demonstrate the basics of
/// .NET reflection. This class contains private member vars,
/// a constructor, a sample method, and sample accessor methods.
/// </summary>
public class Reflect
{
// private member variables
private int intTest;
private string strTest;

// constructor
public Reflect(int intData)
{
// constructor logic
intTest = intData;
}

// basic method
public int AMethod(int intData)
{
return intData*intData;
}

public string S
{
get
{
// return member var
return strTest;
}
set
{
// set member var
S = value;
}
}
}
}

正如你所看到的那样,这个类包含一个构造器,一个例子方法,一个例子存取方法(得到和设置)。在System.Reflaction 名字空间核心是一个名为type的类。这个type类型包含许多方法和到许多类信息的一种登录入口。Type类参考可以靠typeof或者GetType的方法得到。下表列举了一些类类型的方法,包括示例GetTypeof 和typeof方法的代码段。
Name
Description

GetFields()
Returns an array of FieldInfo objects describing the fields in the class.

GetMethods()
Returns an array of MethodInfo objects describing the methods of the class.

GetConstructors()
Returns all the constructors for an object.

GetInterfaces()
Gets all interfacs implemented by the type.

GetMembers()
Gets all the members (fields, methods, events, etc.) of the current type.

InvokeMember()
Invokes a member of the current type.

BaseType
Gets a Type object for the type's immediate base type.

IsAbstract
Returns true if the type is abstract.

IsClass
Returns true if the type is a class.

IsEnum
Returns true if the type is an enum.

IsNestedPublic
Returns true if a type is nested within another and is public.

Namespace
Retrieves the namespace of the type.



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);

}
}
}


上一篇:.NET语言的选择  人气:12427
下一篇:.net的reflection (2) 人气:11272
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐