System.Reflection.MethodBase Class
Namespace
System.Reflection
Assembly
System.Private.CoreLib.dll
Summary
Represents methods and constructors. This abstract class provides information about method signatures, calling conventions, custom attributes, and security information.
Syntax
public abstract class MethodBase : MemberInfo
Inheritance
Object → MemberInfo → MethodBase
Members
Member | Type | Description |
---|---|---|
IsPublic | bool | Gets a value indicating whether the method is public. |
IsStatic | bool | Gets a value indicating whether the method is static. |
Invoke | object | Invokes the method or constructor represented by this instance. |
GetCustomAttributes | object[] | Returns an array of custom attributes applied to this method. |
GetParameters | ParameterInfo[] | Returns the parameters of the method. |
Remarks
The MethodBase
class provides a base for MethodInfo
(which represents methods) and ConstructorInfo
(which represents constructors). It enables inspection of method metadata at runtime, which is essential for scenarios such as dynamic type generation, serialization, and custom attribute processing.
Example
// Display the names of all public instance methods of a type
using System;
using System.Reflection;
class Demo
{
static void Main()
{
Type t = typeof(String);
MethodBase[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (MethodBase mb in methods)
{
Console.WriteLine($"{mb.ReturnType?.Name ?? "void"} {mb.Name}");
}
}
}