public abstract class MethodBase : MemberInfo
Object
MemberInfo
MethodBase
mscorlib
Reflection
Provides information about methods and constructors.
[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.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
protected MethodBase();
Constructs a new instance of the MethodBase class.
System.Reflection.MethodBase Class, System.Reflection Namespace
public virtual Type[] GetGenericArguments()
Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition.
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.
Exception Type Condition NotSupportedException Default behavior when not overridden in a derived class.
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.
- If the current method is a closed constructed method (that is, the System.Reflection.MethodBase.ContainsGenericParameters property returns
false
), the array returned by the System.Reflection.MethodBase.GetGenericArguments method contains the types that have been assigned to the generic type parameters of the generic method definition.
- If the current method is a generic method definition, the array contains the type parameters.
- If the current method is an open constructed method (that is, the System.Reflection.MethodBase.ContainsGenericParameters property returns
true
) in which specific types have been assigned to some type parameters and type parameters of enclosing generic types have been assigned to other type parameters, the array contains both types and type parameters. Use the System.Type.IsGenericParameter property to tell them apart.
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 the default 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. MemberAccessException The caller does not have permission to execute the method or constructor. InvalidOperationException The type that declares the method is an open generic type. That is, System.Type.ContainsGenericParameters returns true
for the declaring type.
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 the default 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. MemberAccessException The caller does not have permission to execute the method or constructor. InvalidOperationException The type that declares the method is an open generic type. That is, System.Type.ContainsGenericParameters returns true
for the declaring type.
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[]) .
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
public virtual bool ContainsGenericParameters { get; }
Gets a value that indicates whether a generic method contains unassigned generic type parameters.
true
if the current method contains unassigned generic type parameters; otherwisefalse
.
The default behavior, when not overridden in a derived class, is to returnfalse
. 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 typeA<T>
, even though the method itself is not generic. Contrast this with the behavior of the System.Reflection.MethodBase.IsGenericMethod property, which returnsfalse
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.
]
System.Reflection.MethodBase Class, System.Reflection Namespace
public virtual bool IsGenericMethod { get; }
Gets a value that indicates whether the current object is a generic method.
true
if the current object is a generic method; otherwisefalse
.
The default behavior, when not overridden in a derived class, is to returnfalse
. 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.
]
System.Reflection.MethodBase Class, System.Reflection Namespace
public virtual bool IsGenericMethodDefinition { get; }
Gets a value that indicates whether the current System.Reflection.MethodBase represents a definition of a generic method.
true
if the current MethodBase object represents the definition of a generic method; otherwisefalse
.
The default behavior, when not overridden in a derived class, is to returnfalse
. 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.
- System.Reflection.MethodBase.IsGenericMethodDefinition returns
true
.
- For each Type object in the array returned by the System.Reflection.MethodBase.GetGenericArguments method: The System.Type.IsGenericParameter property returns
true
; the System.Type.DeclaringMethod returns the current instance; and the System.Type.GenericParameterPosition property is the same as the position of the Type object in the array.
[Behaviors: This property is read-only.
]
System.Reflection.MethodBase Class, System.Reflection Namespace