public abstract class MethodBase : MemberInfo
Object
MemberInfo
MethodBase
mscorlib
Reflection
Provides access to method and constructor metadata.
[Note:MethodBase
is used to represent method types.The Base Class Library includes the following derived types:
]
System.Reflection Namespace
MethodBase Constructors
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
protected MethodBase();
Constructs a new instance of the MethodBase class.
System.Reflection.MethodBase Class, System.Reflection Namespace
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle);
Gets method information by using the method's internal metadata representation (handle).
- handle
- The method's RuntimeMethodHandle handle.
A MethodBase object containing information about the method.
The handles are valid only in the application domain in which they were obtained.
RuntimeInfrastructure
System.Reflection.MethodBase Class, System.Reflection Namespace
public abstract ParameterInfo[] GetParameters();
Returns the parameters of the method or constructor reflected by the current instance.
An array of ParameterInfo objects that contain information that matches the signature of the method or constructor reflected by the current instance.
[Behaviors: As described above.]
System.Reflection.MethodBase Class, System.Reflection Namespace
public abstract object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture);
Invokes the method or constructor reflected by the current instance as determined by the specified arguments.
- 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 isnull
, 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 isnull
. For value-type elements, this value is 0, 0.0, orfalse
, depending on the specific element type. If the method or constructor reflected by the current instance isstatic
, this parameter is ignored.]
- culture
- The only defined value for this parameter is
null
.
A Object that contains the return value of the invoked method, or a re-initialized object if a constructor was invoked.
Exception Type Condition ArgumentException The 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 . TargetException The 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.TargetInvocationException The method reflected by the current instance threw an exception. TargetParameterCountException parameters.Length does not equal the number of parameters required by the contract of the constructor or method reflected by the current instance.
Optional parameters can not be omitted in calls to System.Reflection.MethodBase.Invoke(System.Object,System.Object[]) .
System.Reflection.MethodBase Class, System.Reflection Namespace
public object Invoke(object obj, object[] parameters);
Invokes the method or constructor reflected by the current instance on the specified object and using the specified arguments.
- 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 isnull
. For value-type elements, this value is 0, 0.0, orfalse
, depending on the specific element type. If the method or constructor reflected by the current instance isstatic
, this parameter is ignored.]
A Object that contains the return value of the invoked method, or a re-initialized object if a constructor was invoked.
Exception Type Condition ArgumentException The 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 . TargetException The 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.TargetInvocationException The constructor or method reflected by the current instance threw an exception. TargetParameterCountException parameters.Length does not equal the number of parameters required by the contract of the member reflected by the current instance.
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[]) .
System.Reflection.MethodBase Class, System.Reflection Namespace
public abstract MethodAttributes Attributes { get; }
Gets the attributes of the method reflected by the current instance.
A MethodAttributes value that signifies the attributes of the method reflected by the current instance.
[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.]
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
System.Reflection.MethodBase Class, System.Reflection Namespace