15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

342 ❘ ChaPTer 14 reflectiOn<br />

The member methods of Type that follow the same pattern are shown in the following table.<br />

TyPe of objeCT reTurned<br />

ConstructorInfo<br />

EventInfo<br />

FieldInfo<br />

MemberInfo<br />

MethodInfo<br />

PropertyInfo<br />

meThods (The meThod WiTh The Plural name reTurns an array)<br />

GetConstructor(), GetConstructors()<br />

GetEvent(), GetEvents()<br />

GetField(), GetFields()<br />

GetMember(), GetMembers(), GetDefaultMembers()<br />

GetMethod(), GetMethods()<br />

GetProperty(), GetProperties()<br />

The GetMember() <strong>and</strong> GetMembers() methods return details of any or all members of the data type,<br />

regardless of whether these members are constructors, properties, methods, <strong>and</strong> so on.<br />

The TypeView example<br />

This section demonstrates some of the features of the Type class with a<br />

short example, TypeView, which you can use to list the members<br />

of a data type. The example demonstrates how to use TypeView for a<br />

double; however, you can swap this type with any other data type just by<br />

changing one line of the code in the sample. TypeView displays far more<br />

information than can be displayed in a console window, so we’re going<br />

to take a break from our normal practice <strong>and</strong> display the output in a<br />

message box. Running TypeView for a double produces the results shown<br />

in Figure 14-1.<br />

The message box displays the name, full name, <strong>and</strong> namespace of the data<br />

type as well as the name of the underlying type <strong>and</strong> the base type. Next,<br />

it simply iterates through all the public instance members of the data<br />

type, displaying for each member the declaring type, the type of member<br />

(method, field, <strong>and</strong> so on), <strong>and</strong> the name of the member. The declaring<br />

type is the name of the class that actually declares the type member (for<br />

example, System.Double if it is defined or overridden in System.Double,<br />

or the name of the relevant base type if the member is simply inherited<br />

from some base class).<br />

TypeView does not display signatures of methods because you are<br />

retrieving details of all public instance members through MemberInfo<br />

objects, <strong>and</strong> information about parameters is not available through<br />

a MemberInfo object. To retrieve that information, you would need<br />

references to MethodInfo <strong>and</strong> other more specific objects, which<br />

means that you would need to obtain details of each type of member<br />

separately.<br />

TypeView does display details of all public instance members, but for<br />

doubles, the only ones defined are fields <strong>and</strong> methods. For this example,<br />

you will compile TypeView as a console application — there is no figure 14-1<br />

problem with displaying a message box from a console application.<br />

However, the fact that you are using a message box means that<br />

you need to reference the base class assembly System.Windows.Forms.dll, which contains the<br />

classes in the System.Windows.Forms namespace in which the MessageBox class that you will<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!