Tải bản đầy đủ - 0 (trang)
3-15. Programmatically Discover the Members of a Type

3-15. Programmatically Discover the Members of a Type

Tải bản đầy đủ - 0trang

CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



Solution

Obtain the System.Type for the type you need to reflect on, and call the GetMembers method to get

information about all of the members of the type, or call the GetConstructors, GetMethods, and

GetProperties methods to get information about specific categories of a member.



How It Works

You can get details of the members of a given type through the System.Type class. You can get an

instance of Type by calling the GetType method on any object or by using the typeof keyword. Once you

have a Type instance, the GetMembers method returns information about all members, while the

GetConstructors, GetMethods, and GetProperties methods return just constructors, methods, and

properties, respectively. The Type class also contains methods to get less commonly used member types

(such as events) and interfaces that the type implements.

The GetMembers method returns an array of System.Reflection.MemberInfo instances, one for each

member in the type you are reflecting on. You can differentiate between different categories of member

using the MemberInfo.MemberType property, as shown by the following fragment:

MemberInfo[] members = myType.GetMembers();

foreach (MemberInfo member in members)

{

switch (member.MemberType)

{

case MemberTypes.Constructor:

// Do something.

break;

case MemberTypes.Method:

// Do something.

break;

}

}

The other System.Type methods return arrays of classes from the System.Reflection namespace

specific to the member category—for example, GetConstructors returns an array of

System.Reflection.ConstructorInfo and GetMethods returns an array of System.Reflection.MethodInfo.



The Code

The following example uses reflection on its own type to enumerate the constructors, methods and

properties.

using

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Reflection;



138



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



namespace Apress.VisualCSharpRecipes.Chapter03

{

class Recipe03_15

{

static void Main(string[] args)

{

// Get the type we are interested in.

Type myType = typeof(Recipe03_15);

// Get the constructor details.

Console.WriteLine("\nConstructors...");

foreach (ConstructorInfo constr in myType.GetConstructors())

{

Console.Write("Constructor: ");

// Get the paramters for this constructor.

foreach (ParameterInfo param in constr.GetParameters())

{

Console.Write("{0} ({1}), ", param.Name, param.ParameterType);

}

Console.WriteLine();

}

// Get the method details.

Console.WriteLine("\nMethods...");

foreach (MethodInfo method in myType.GetMethods())

{

Console.Write(method.Name);

// Get the paramters for this constructor.

foreach (ParameterInfo param in method.GetParameters())

{

Console.Write("{0} ({1}), ", param.Name, param.ParameterType);

}

Console.WriteLine();

}

// Get the property details.

Console.WriteLine("\nProperty...");

foreach (PropertyInfo property in myType.GetProperties())

{

Console.Write("{0} ", property.Name);

// Get the paramters for this constructor.

foreach (MethodInfo accessor in property.GetAccessors())

{

Console.Write("{0}, ", accessor.Name);

}

Console.WriteLine();

}



139



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

public string MyProperty

{

get;

set;

}

public Recipe03_15(string param1, int param2, char param3)

{

}

}

}



3-16. Invoke a Type Member Using Reflection

Problem

You need to invoke a method on a type.



Solution

Use the InvokeMember method of System.Type or the Invoke method on the MemberInfo class or its derived

types (MethodInfo, PropertyInfo, etc.).



How It Works

You can call a member directly on a Type, using the InvokeMember method, to which you must supply the

name of the method you wish to call, the instance of the type you wish to call against, and an array of

objects containing the parameters you wish to pass to the member. You must also provide a value from

the BindingFlags enumeration that specifies what kind of call should be made—values exist for invoking

a method (BindingFlags.InvokeMethod), getting and setting properties (BindingFlags.GetProperty and

BindingFlags.SetProperty), and so on.

You can obtain an instance of MemberInfo or one of its derived types (such as MethodInfo for

methods, PropertyInfo for properties, etc.) and call the Invoke method. See the code for this recipe for

an example of both approaches.



The Code

The following example calls a method using the InvokeMember method of System.Type and calls it again

through the MemberInfo class:



140



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



using System;

using System.Reflection;

namespace Apress.VisualCSharpRecipes.Chapter03

{

class Recipe03_16

{

static void Main(string[] args)

{

// Create an instance of this type.

object myInstance = new Recipe03_16();

// Get the type we are interested in.

Type myType = typeof(Recipe03_16);

// Get the method information.

MethodInfo methodInfo = myType.GetMethod("printMessage",

new Type[] { typeof(string), typeof(int), typeof(char) });

// Invoke the method using the instance we created.

myType.InvokeMember("printMessage", BindingFlags.InvokeMethod,

null, myInstance, new object[] { "hello", 37, 'c' });

methodInfo.Invoke(null, BindingFlags.InvokeMethod, null,

new object[] { "hello", 37, 'c' }, null);

// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

public static void printMessage(string param1, int param2, char param3)

{

Console.WriteLine("PrintMessage {0} {1} {2}", param1, param2, param3);

}

}

}



141



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

3-15. Programmatically Discover the Members of a Type

Tải bản đầy đủ ngay(0 tr)

×