MethodBase Class
Represents methods (including constructors) in the reflection system.
Syntax
public abstract class MethodBase : MemberInfo
Inheritance
System.Object
↳ System.Reflection.MemberInfo
↳ System.Reflection.MethodBase
Summary
The MethodBase
class provides information about methods and constructors (including access level, parameters, generic arguments, and custom attributes). It also enables the dynamic invocation of a method or constructor.
Properties
Name | Type | Description |
---|---|---|
IsAbstract | bool | Gets a value indicating whether the method is abstract. |
IsConstructor | bool | Gets a value indicating whether the method is a constructor. |
IsFamily | bool | Gets a value indicating whether the method is protected. |
IsPublic | bool | Gets a value indicating whether the method is public. |
IsStatic | bool | Gets a value indicating whether the method is static. |
Methods
Name | Return Type | Signature |
---|---|---|
Invoke |
object | public object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) |
GetParameters |
ParameterInfo[] | public ParameterInfo[] GetParameters() |
GetCustomAttributes |
object[] | public override object[] GetCustomAttributes(bool inherit) |
Example
Use MethodBase
to call a private method dynamically:
using System;
using System.Reflection;
class Demo
{
private void Secret(string msg)
{
Console.WriteLine($"Secret: {msg}");
}
static void Main()
{
var d = new Demo();
MethodInfo mi = typeof(Demo).GetMethod("Secret", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(d, new object[] { "Hello from reflection!" });
}
}