Working with Method Information


Method information centers on the MethodInfo class. You can obtain an instance of this through the GetMethod or GetMethods methods on the Type object, as shown in the following example:

Type t = cust.GetType(); MethodInfo method = t.GetMethod("DoSomething"); 


Before diving into a code example, let's take a look at Table 11.2, which lists some of the common properties and methods available for the MethodInfo class.

Table 11.2. MethodInfo Properties and Methods

Property/Method

Description

Attributes

Gets the list of attributes attached to the method.

IsAbstract

Indicates whether the method is abstract.

IsAssembly

Indicates whether the method can be called by other classes contained within the assembly.

IsConstructor

Indicates whether the method is a constructor.

IsFinal

Indicates whether the method has been sealed and made final.

Name

Gets the name of the method.

ReturnType

Indicates the return type of the method.

GetMethodBody()

Obtains information about the inside of the method itself, such as the type and amount of local variables, exception-handling blocks, and so on.

GetParameters()

Gets the list of parameters for the method.

MakeGenericMethod

Replaces the open generic parameters for the method with the supplied array of types, binding the method to the types at runtime and returning a method that can then be executed.


Now let's see some of these methods and properties in action, as shown in the sample code in Listing 11.1.

Listing 11.1. Reflecting on Methods and Parameters

[View full width]

using System; using System.Reflection; using System.Collections.Generic; using System.Text; namespace ReflectMethods { class Program { static void Main(string[] args) {     Customer<string> cust = new Customer<string>();     Type t = cust.GetType();     MethodInfo method = t.GetMethod("DoSomething");     Console.WriteLine(method.Name);     Console.WriteLine("--------------------------");     Console.WriteLine("Abstract: {0}, Visible to Assembly: {1}, Constructor: {2}, Sealed:  {3}",         method.IsAbstract, method.IsAssembly, method.IsConstructor, method.IsFinal);     Console.WriteLine("Generic: {0}, Virtual: {1}, Static: {2}",         method.IsGenericMethod, method.IsVirtual, method.IsStatic);     Console.WriteLine("{0} returns {1}",         method.Name, method.ReturnType.ToString());     Console.WriteLine("Parameters:");     foreach (ParameterInfo p in method.GetParameters())     {         Console.WriteLine("\t{0} : {1} [{2}]{3}",             p.Name, p.ParameterType.ToString(),             p.IsOut ? "Out" : "In",             p.ParameterType.IsGenericParameter ? "* Generic" : "");     }     Console.WriteLine("Local variables within method:");     foreach (LocalVariableInfo lvi in method.GetMethodBody().LocalVariables)     {         Console.WriteLine(lvi.ToString());     }     Console.ReadLine(); } } } 

The output of the preceding code looks as follows:

DoSomething -------------------------- Abstract: False, Visible to Assembly: False, Constructor: False, Sealed: False Generic: True, Virtual: False, Static: False DoSomething returns System.String Parameters:         input : U [In]* Generic         otherCustomer : ReflectMethods.Customer`1[System.String]& [In]         newCustomer : ReflectMethods.Customer`1[System.String]& [Out]         anotherInput : System.Int32 [In] Local variables within method: System.Int32 (0) System.String (1) System.String (2) 


The Customer class on which the preceding code is reflecting is shown here:

using System; using System.Collections.Generic; using System.Text; namespace ReflectMethods { class Customer<T> { private int privData = 12; public int PubData {     get { return privData; }     set { privData = value; } } public string DoSomething<U>(U input, ref Customer<T> otherCustomer,     out Customer<T> newCustomer,     int anotherInput) {     int z = 21;     string localString = "local";     newCustomer = new Customer<T>();     return input.ToString(); } } } 


One thing that might be somewhat surprising for developers who have prior experience with reflection is that generics are completely compatible with reflection. Using reflection, you can obtain all information about a method, even information related to generic type parameters. Also, if you look back at the otherCustomer and newCustomer parameters in the console output, you can see that those parameters aren't indicated as being generic.

In fact, you can see that as far as the runtime is concerned, those parameters are of type Customer<string>.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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