System.Reflection.MethodBase Class

public abstract class MethodBase : MemberInfo

Base Types

Object
  MemberInfo
    MethodBase

Assembly

mscorlib

Library

Reflection

Summary

Provides information about methods and constructors.

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.GetGenericArguments Method
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.ContainsGenericParameters Property
MethodBase.IsGenericMethod Property
MethodBase.IsGenericMethodDefinition Property


MethodBase Constructor

protected MethodBase();

Summary

Constructs a new instance of the MethodBase class.

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.GetGenericArguments Method

public virtual Type[] GetGenericArguments()

Summary

Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition.

Return Value

An array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. Returns an empty array if the current method is not a generic method.

Exceptions

Exception TypeCondition
NotSupportedExceptionDefault behavior when not overridden in a derived class.

Description

The default behavior, when not overridden in a derived class, is to throw NotSupportedException. In other words, derived classes do not support generics by default.

The elements of the returned array are in the order in which they appear in the list of type parameters for the generic method.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

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 the default 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.
MemberAccessExceptionThe caller does not have permission to execute the method or constructor.
InvalidOperationExceptionThe type that declares the method is an open generic type. That is, System.Type.ContainsGenericParameters returns true for the declaring type.

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 the default 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.
MemberAccessExceptionThe caller does not have permission to execute the method or constructor.
InvalidOperationExceptionThe type that declares the method is an open generic type. That is, System.Type.ContainsGenericParameters returns true for the declaring type.

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 cannot 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

MethodBase.ContainsGenericParameters Property

public virtual bool ContainsGenericParameters { get; }

Summary

Gets a value that indicates whether a generic method contains unassigned generic type parameters.

Property Value

true if the current method contains unassigned generic type parameters; otherwise false .

Description

The default behavior, when not overridden in a derived class, is to return false . In other words, by default, derived classes do not support generics.

In order to invoke a generic method, there must be no generic type definitions or open constructed types in the type arguments of the method itself, or in any enclosing types. If the System.Reflection.MethodBase.ContainsGenericParameters property returns true , the method cannot be invoked.

The System.Reflection.MethodBase.ContainsGenericParameters property searches recursively for type parameters. For example, it returns true for any method in an open type A<T> , even though the method itself is not generic. Contrast this with the behavior of the System.Reflection.MethodBase.IsGenericMethod property, which returns false for such a method.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.IsGenericMethod Property

public virtual bool IsGenericMethod { get; }

Summary

Gets a value that indicates whether the current object is a generic method.

Property Value

true if the current object is a generic method; otherwise false .

Description

The default behavior, when not overridden in a derived class, is to return false . In other words, by default, derived classes do not support generics.

Use this property to determine whether the current MethodBase object represents a generic method. Use the System.Reflection.MethodBase.ContainsGenericParameters property to determine whether the current MethodBase object represents an open constructed method or a closed constructed method.

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace

MethodBase.IsGenericMethodDefinition Property

public virtual bool IsGenericMethodDefinition { get; }

Summary

Gets a value that indicates whether the current System.Reflection.MethodBase represents a definition of a generic method.

Property Value

true if the current MethodBase object represents the definition of a generic method; otherwise false .

Description

The default behavior, when not overridden in a derived class, is to return false . In other words, by default, derived classes do not support generics.

If the current MethodBase represents a generic method definition, then:

For a list of the invariant conditions for terms specific to generic methods, see the System.Reflection.MethodInfo.IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the System.Type.IsGenericType property.

[Behaviors: This property is read-only.

]

See Also

System.Reflection.MethodBase Class, System.Reflection Namespace