Thursday, October 19, 2006

Reflecting types

The Type class有什么用?

@ The Type class represents a single type, allowing you to look at the methods, properties, events, interfaces, and inheritance tree of a particular type in the system.

How to get the Type object?

@ You can get Type object in 4 ways:

  1. From the Assembly class
  2. From the Module class
  3. From the instance of an object
  4. Using keyword typeof in C# or GetType in VB.

@ 1st way: from the Assembly class:

Dim OneAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim AssemblyTypes() As Type = OneAssembly.GetTypes()

@ 2nd way: from the Module class:

Dim AllModulesOfOneAssembly As [Module] = OneAssembly.GetModules()
Dim OneModuleOfOneAssembly As [Module] = AllModulesOfOneAssembly(0)
Dim AllTypesOfOneModuleOfOneAssembly () As Type = OneModuleOfOneAssembly .GetTypes()

@ 3rd way: get the type of the instance of an object by calling its GetType method:

Dim OneObject As New Object()
Dim TypeOfOneObject As Type = OneObject.GetType()

@ 4th way: create a Type object using C# typeof / VB GetType keywork:

Dim OneSpecificType As Type = GetType(Int32) 'VB

Type OneSpecificType = typeof(Int32) //C#