久久精品综合-欧美精品久久久久久-www一区二区-韩国三级中文字幕hd久久精品-国产一区二区三区在线-日韩免费视频

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

.net的reflection (1) 

[摘要]在我的文章《C#基于接口的編程》中,我談?wù)摿耸褂贸橄蟮慕涌谧鳛榫幊谭独母鞣N優(yōu)點(diǎn)。分離接口和執(zhí)行過程,現(xiàn)在肯定不是新的思想,實(shí)際上它是com編程的核心。也許基于接口的在顯著的特征是多態(tài)性和即時(shí)檢查(RTTI).RTTI允許客戶端程序在運(yùn)行時(shí)訪問對(duì)象。例如,如果一個(gè)對(duì)象執(zhí)行IAthlete接口,一個(gè)...
在我的文章《C#基于接口的編程》中,我談?wù)摿耸褂贸橄蟮慕涌谧鳛榫幊谭独母鞣N優(yōu)點(diǎn)。分離接口和執(zhí)行過程,現(xiàn)在肯定不是新的思想,實(shí)際上它是com編程的核心。也許基于接口的在顯著的特征是多態(tài)性和即時(shí)檢查(RTTI).RTTI允許客戶端程序在運(yùn)行時(shí)訪問對(duì)象。例如,如果一個(gè)對(duì)象執(zhí)行IAthlete接口,一個(gè)客戶端程序能夠查找然后綁定這個(gè)接口用于調(diào)用和定位另一個(gè)接口。
查詢接口是強(qiáng)大的,是com+的基礎(chǔ)。同時(shí),它對(duì)能夠在一個(gè)對(duì)象上執(zhí)行接口導(dǎo)向又是極端有用的和明確必要的,這是.net一個(gè)特別重要的概念。在介紹這個(gè)概念之前,讓我們?cè)倩仡櫼恍┬畔㈥P(guān)于在.net中功能性和分布的基礎(chǔ)元素--集合。
在COM(+),組件展開的邏輯和物理單元是一個(gè).dll文件。在.net平臺(tái)中,展開的基礎(chǔ)單元是集合。與COM(+)組件不同的是一個(gè).net或許是有多個(gè)文件組成,盡管它被看作是一個(gè)單一的何不可分割的功能和執(zhí)行單元。一個(gè)集合文件也是自描述的,它包含被稱之為中間語(yǔ)言的管制代碼和附加的元數(shù)據(jù),這個(gè)元數(shù)據(jù)給想綁定到這些代碼的客戶提供豐富的信息。.net的每個(gè)集合是著名的清單數(shù)據(jù)結(jié)構(gòu)。清單包含下列信息:
1、姓名和版本信息
2、其它集合的參考信息
3、安全邊界的信息。
4、集合中所有類型的信息

毫無(wú)疑問,集合清單類似于COM(+)的類型庫(kù)。清單有在類型庫(kù)中不存在的優(yōu)點(diǎn)。這個(gè)超過了這一課的范圍,我們將在這篇文章的第二部分進(jìn)行講述。被利用.net集合的客戶端程序?qū)⒁褂们鍐巍R驗(yàn)槊總(gè)集合包含元數(shù)據(jù)的許多信息,客戶程序在運(yùn)行時(shí)能夠得到集合的內(nèi)容。這個(gè)過程就叫做reflection.Reflection不僅能夠查詢基礎(chǔ)類,而且運(yùn)行時(shí)能動(dòng)態(tài)調(diào)用方法,甚至于輸出能夠直接執(zhí)行的IL代碼。在你的代碼中有兩種方法來(lái)使用reflection,Reflection API 和 System.Reflection 名字空間。System.reflection是極端負(fù)責(zé)的,它包含將近40個(gè)類。在文章的其他部分,我將介紹reflection的基礎(chǔ),如何用它區(qū)查詢集合類,方法,以及運(yùn)行時(shí)調(diào)用方法的類型。為了示范在.net中的reflection名字空間,我將用下面的"reflection"類來(lái)說明:

// 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;
}
}
}
}

正如你所看到的那樣,這個(gè)類包含一個(gè)構(gòu)造器,一個(gè)例子方法,一個(gè)例子存取方法(得到和設(shè)置)。在System.Reflaction 名字空間核心是一個(gè)名為type的類。這個(gè)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);

}
}
}





標(biāo)簽:.net的reflection (1)  

相關(guān)文章

主站蜘蛛池模板: 三级国产在线 | 亚洲欧美在线观看一区二区 | 四虎免费大片aⅴ入口 | 亚洲国产综合精品中文字幕 | 一级做a毛片免费视频 | 午夜久久精品 | 色噜噜狠狠色综合欧洲 | 日韩毛片免费视频 | 亚洲视频免费在线观看 | 天天在线天天综合网色 | 天天久 | 四虎最新网址 | 亚欧有色 | 色综合天天综久久久噜噜噜久久〔 | 色综合色狠狠天天综合色hd | 青草影院在线观看 | 亚洲第二色 | 日韩一卡2卡3卡新区网站 | 伊久久 | 青娱乐最新视频 | 亚洲第一页在线视频 | 亚洲福利一区二区 | 亚洲成人自拍 | 午夜短视频 | 欧美一级高清片在线 | 日韩午夜伦| 日本一片免费观看高清完整 | 亚洲福利一区二区精品秒拍 | 天天影视色香欲综合影视大全 | 污污在线观看视频 | 日本成人在线看 | 天堂在线影院 | 日本一本在线 | 日本不卡免费新一区二区三区 | 午夜网站在线观看免费网址免费 | 天堂网在线最新版www中文网 | 日日夜干 | 日本精品高清一区二区2021 | 日本三级香港三级人妇99 | 香港三级理论在线播放1 | 青色影院|