Discovering Type Information


The System.Type class is the primary means by which type metadata is accessed. It provides an API that will allow you to manipulate newly discovered types. A System.Type object instance can represent all .NET languages types, including classes, interfaces, arrays, values, and enumerations.

First, you will need to get a reference to a System.Type object instance. There are several different ways to do this. In this section we will discuss two of the most popular.

Retrieving Type Information with the GetType Method

The easiest way to get a reference to a System.Type object instance is to call the GetType method on an already existing object instance. The GetType method is a public virtual method declared on the Object class. This means that every .NET class inherits this method, and in turn , this gives you access to its underlying type. Listing 13.2 shows how to use the GetType method.

Listing 13.2
 C# string str = "Hello, Reflection!"; Type strType = str.GetType(); MessageBox.Show(strType.ToString(),"Name of String Type"); VB Dim str as string Dim strType as Type str = "Hello, world!" strType = str.GetType() MessageBox.Show(strType.ToString(),"Name of String Type") 

RETRIEVING TYPE INFORMATION WITH THE C# typeof OPERATOR

If you are using C#, then you can use the typeof operator to retrieve the System.Type object. A typeof expression takes the form typeof( type ) . The type parameter is the name of the type. This is not the name of the type contained in a .NET string. Rather, it is the name of the type without quotation marks. Listing 13.3 exemplifies how to use the typeof operator.

Listing 13.3
 C# public class TypeOf_Operator {   public int integer;   public TypeOfOperator () {   } } public class Test {   public static void Main() {     Type t = typeof(TypeOf_Operator);     MessageBox.Show(t.ToString());   } } 

It is important to realize that the typeof operator is not resolved at compile time. There is work performed at runtime to return the correct System.Type object instance. In some cases this work can be quite expensive, so do not choose the typeof operator over the GetType method under the assumption that the typeof operator will perform more efficiently . They both require a substantial amount of work to ensure that the correct System.Type object instance is returned.


Retrieving Type Information from a Loaded Assembly

You can also load a type from a loaded assembly. This is very valuable if you do not know the name of the type you are loading or if you do not have an instance of the type you are working with. The GetTypes method will return an array of System.Type objects that contains all of the types found in the assembly. Listing 13.4 demonstrates how to use this method.

Listing 13.4
 C# Assembly anAssembly = Assembly.LoadFrom("\sample.dll"); Type[] types = anAssembly.GetTypes(); foreach(Type type in types)   MessageBox.Show("Found " + type.ToString()); VB Dim anAssembly as Assembly.LoadFrom("\sample.dll") Dim Types[] as anAssembly.GetTypes() for (i = 0 to types.Length)   MessageBox.Show("Found " & types[i].ToString()) Next i 

The GetType method on the Assembly class allows you to retrieve a specific System.Type from the assembly if you know the type's name. This method is an overload of the GetType method inherited from the object class. The GetType method on the Assembly class takes the name of the type as a parameter and searches the Assembly for a type with the matching name. If one is found, a System.Type object instance representing that type is returned. Otherwise, null is returned. Listing 13.5 demonstrates how to use the GetType method.

Listing 13.5
 C# Assembly anAssembly = Assembly.LoadFrom("\sample.dll"); Type type = anAssembly.GetType("TypeClass"); MessageBox.Show("Found " + type.ToString()); VB Dim anAssembly as Assembly.LoadFrom("\sample.dll") Dim Type as anAssembly.GetType("TypeClass") MessageBox.Show("Found " & type.ToString()) 

There is a similar static GetType defined on the Type class. This GetType method also takes the type name as a parameter. The name of the type should be assembly name qualified because the method needs to know in which assembly to search. An assembly qualified name has the following format:

 
 TopNamespace.SubNamespace.Class+NestedClass,AssembleName 

Listing 13.6 shows how to use the Type.GetType with an assembly-qualified type name.

Listing 13.6
 C# Type type = Type.GetType("TypeClass,Sample"); MessageBox.Show("Found " + type.ToString()); VB Dim Type as anAssembly.GetType("TypeClass, Sample") MessageBox.Show("Found " & type.ToString()) 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net