System.Reflection.MethodBase Class

public abstract class MethodBase : MemberInfo

Base Types

Object
  MemberInfo
    MethodBase

Assembly

mscorlib

Library

Reflection

Summary

Provides access to method and constructor metadata.

Description

[Note: MethodBase is used to represent method types.

The Base Class Library includes the following derived types:

]

See Also

System.Reflection Namespace

Members

MethodBase Constructors

MethodBase Constructor

MethodBase Methods

MethodBase.GetMethodFromHandle Method
MethodBase.GetParameters Method
MethodBase.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo) Method
MethodBase.Invoke(System.Object, System.Object[]) Method

MethodBase Properties

MethodBase.Attributes Property


MethodBase Constructor

protected MethodBase();

Summary

Constructs a new instance of the MethodBase class.

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.GetMethodFromHandle Method

public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle);

Summary

Gets method information by using the method's internal metadata representation (handle).

Parameters

handle
The method's RuntimeMethodHandle handle.

Return Value

A MethodBase object containing information about the method.

Description

The handles are valid only in the application domain in which they were obtained.

Library

RuntimeInfrastructure

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.GetParameters Method

public abstract ParameterInfo[] GetParameters();

Summary

Returns the parameters of the method or constructor reflected by the current instance.

Return Value

An array of ParameterInfo objects that contain information that matches the signature of the method or constructor reflected by the current instance.

Description

[Behaviors: As described above.]

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo) Method

public abstract object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture);

Summary

Invokes the method or constructor reflected by the current instance as determined by the specified arguments.

Parameters

obj
An instance of the type that contains the method reflected by the current instance. If the method is static, obj is ignored. For non-static methods, obj is an instance of a class that inherits or declares the method.
invokeAttr
A BindingFlags value that controls the binding process.
binder
An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is null , the default binder is used.
parameters
An array of objects that match the number, order and type of the parameters for the constructor or method reflected by the current instance. If the member reflected by the current instance takes no parameters, specify either an array with zero elements or null . [Note: Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null . For value-type elements, this value is 0, 0.0, or false , depending on the specific element type. If the method or constructor reflected by the current instance is static , this parameter is ignored.]

culture
The only defined value for this parameter is null .

Return Value

A Object that contains the return value of the invoked method, or a re-initialized object if a constructor was invoked.

Exceptions

Exception TypeCondition
ArgumentExceptionThe types of the elements of parameters do not match the types of the parameters accepted by the constructor or method reflected by the current instance, under the constraints of binder .
TargetExceptionThe constructor or method reflected by the current instance is non-static, and obj is null or is of a type that does not implement the member reflected by the current instance.
TargetInvocationExceptionThe method reflected by the current instance threw an exception.
TargetParameterCountExceptionparameters.Length does not equal the number of parameters required by the contract of the constructor or method reflected by the current instance.

Description

Optional parameters can not be omitted in calls to System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) .

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.Invoke(System.Object, System.Object[]) Method

public object Invoke(object obj, object[] parameters);

Summary

Invokes the method or constructor reflected by the current instance on the specified object and using the specified arguments.

Parameters

obj
An instance of a type that contains the constructor or method reflected by the current instance. If the member is static, obj is ignored. For non-static methods, obj is an instance of a class that inherits or declares the method.
parameters
An array objects that match the number, order and type of the parameters for the constructor or method reflected by the current instance. If the member reflected by the current instance takes no parameters, specify either an array with zero elements or null . [Note: Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null . For value-type elements, this value is 0, 0.0, or false , depending on the specific element type. If the method or constructor reflected by the current instance is static , this parameter is ignored.]

Return Value

A Object that contains the return value of the invoked method, or a re-initialized object if a constructor was invoked.

Exceptions

Exception TypeCondition
ArgumentExceptionThe types of the elements of parameters do not match the types of the parameters accepted by the constructor or method reflected by the current instance, under the constraints of binder .
TargetExceptionThe constructor or method reflected by the current instance is non-static, and obj is null or is of a type that does not implement the member reflected by the current instance.
TargetInvocationExceptionThe constructor or method reflected by the current instance threw an exception.
TargetParameterCountExceptionparameters.Length does not equal the number of parameters required by the contract of the member reflected by the current instance.

Description

This version of System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) is equivalent to System.Reflection.MethodBase.Invoke(System.Object,System.Object[])(obj, (BindingFlags )0, null , parameters, null ).

Optional parameters can not be omitted in calls to System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) .

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.Attributes Property

public abstract MethodAttributes Attributes { get; }

Summary

Gets the attributes of the method reflected by the current instance.

Property Value

A MethodAttributes value that signifies the attributes of the method reflected by the current instance.

Description

[Behaviors: This property is read-only.

This property gets a MethodAttributes value that indicates the attributes set in the metadata of the method reflected by the current instance.

]

[Usage: Use this property to determine the accessibility, layout, and semantics of the constructor or method reflected by the current instance. Also use this property to determine if the member reflected by the current instance is implemented in native code or has a special name.]

Example

The following example demonstrates using this property to obtain the attributes of three methods.

using System;
using System.Reflection;

abstract class MyBaseClass
{

   abstract public void MyPublicInstanceMethod();

}

class MyDerivedClass : MyBaseClass
{

   public override void MyPublicInstanceMethod() {}
   private static void MyPrivateStaticMethod() {}

}

class MethodAttributesExample
{

   static void PrintMethodAttributes(Type t)
   {

      string str;
      MethodInfo[] miAry = t.GetMethods( BindingFlags.Static |
         BindingFlags.Instance | BindingFlags.Public |
         BindingFlags.NonPublic | BindingFlags.DeclaredOnly );
      foreach (MethodInfo mi in miAry)
      {

         Console.WriteLine("Method {0} is: ", mi.Name);
         str = ((mi.Attributes & MethodAttributes.Static) != 0) ?
            "Static" : "Instance";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.Public) != 0) ?
            "Public" : "Not-Public";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.HideBySig) != 0) ?
            "HideBySig" : "Hide-by-name";
         Console.Write(str + " ");
         str = ((mi.Attributes & MethodAttributes.Abstract) != 0) ?
            "Abstract" : String.Empty;
         Console.WriteLine(str);

      }

   }

   public static void Main()
   {

      PrintMethodAttributes(typeof(MyBaseClass));
      PrintMethodAttributes(typeof(MyDerivedClass));

   }

}
      
The output is

Method MyPublicInstanceMethod is:

Instance Public HideBySig Abstract

Method MyPublicInstanceMethod is:

Instance Public HideBySig

Method MyPrivateStaticMethod is:

Static Not-Public HideBySig

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace