15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using reflection ❘ 341<br />

ProPerTy<br />

Name<br />

FullName<br />

Namespace<br />

reTurns<br />

The name of the data type<br />

The fully qualified name of the data type (including the namespace name)<br />

The name of the namespace in which the data type is defined<br />

Second, it is possible to retrieve references to further type objects that represent related classes, as shown in<br />

the following table.<br />

ProPerTy<br />

BaseType<br />

UnderlyingSystemType<br />

reTurns TyPe referenCe CorresPonding To<br />

Immediate base type of this type<br />

The type that this type maps to in the .<strong>NET</strong> runtime (recall that certain .<strong>NET</strong> base<br />

types actually map to specific predefined types recognized by IL)<br />

A number of Boolean properties indicate whether this type is, for example, a class, an enum, <strong>and</strong> so<br />

on. These properties include IsAbstract, IsArray, IsClass, IsEnum, IsInterface, IsPointer,<br />

IsPrimitive (one of the predefined primitive data types), IsPublic, IsSealed, <strong>and</strong> IsValueType. For<br />

example, using a primitive data type:<br />

Type intType = typeof(int);<br />

Console.WriteLine(intType.IsAbstract);<br />

Console.WriteLine(intType.IsClass);<br />

Console.WriteLine(intType.IsEnum);<br />

Console.WriteLine(intType.IsPrimitive);<br />

Console.WriteLine(intType.IsValueType);<br />

Or using the Vector class:<br />

Type vecType = typeof(Vector);<br />

Console.WriteLine(vecType.IsAbstract);<br />

Console.WriteLine(vecType.IsClass);<br />

Console.WriteLine(vecType.IsEnum);<br />

Console.WriteLine(vecType.IsPrimitive);<br />

Console.WriteLine(vecType.IsValueType);<br />

// writes false<br />

// writes false<br />

// writes false<br />

// writes true<br />

// writes true<br />

// writes false<br />

// writes true<br />

// writes false<br />

// writes false<br />

// writes false<br />

Finally, you can also retrieve a reference to the assembly that the type is defined in. This is returned as a<br />

reference to an instance of the System.Reflection.Assembly class, which is examined shortly:<br />

Methods<br />

Type t = typeof (Vector);<br />

Assembly containingAssembly = new Assembly(t);<br />

Most of the methods of System.Type are used to obtain details of the members of the corresponding data<br />

type — the constructors, properties, methods, events, <strong>and</strong> so on. Quite a large number of methods exist, but<br />

they all follow the same pattern. For example, two methods retrieve details of the methods of the data type:<br />

GetMethod() <strong>and</strong> GetMethods(). GetMethod() returns a reference to a System.Reflection.MethodInfo<br />

object, which contains details of a method. GetMethods() returns an array of such references. The<br />

difference is that GetMethods() returns details of all the methods, whereas GetMethod() returns details of<br />

just one method with a specified parameter list. Both methods have overloads that take an extra parameter,<br />

a BindingFlags enumerated value that indicates which members should be returned — for example,<br />

whether to return public members, instance members, static members, <strong>and</strong> so on.<br />

For example, the simplest overload of GetMethods() takes no parameters <strong>and</strong> returns details of all the<br />

public methods of the data type:<br />

Type t = typeof(double);<br />

MethodInfo[] methods = t.GetMethods();<br />

foreach (MethodInfo nextMethod in methods)<br />

{<br />

// etc.<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!