MethodBase.GetMethodFromHandle

Namespace: System.Reflection Assembly: System.Private.CoreLib.dll

Syntax

public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle);
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType);

        

Parameters

NameTypeDescription
handleRuntimeMethodHandleThe handle of the method to retrieve.
declaringTypeRuntimeTypeHandle(Optional) The handle for the type that declares the method. Required for generic methods.

Return value

Returns a MethodBase instance that represents the method identified by the provided handle.

Exceptions

ExceptionCondition
ArgumentNullExceptionIf handle is zero.
ArgumentExceptionIf the method handle is invalid or the declaring type does not match.

Remarks

The returned MethodBase can be cast to MethodInfo or ConstructorInfo depending on the method type. This method is primarily used in low‑level reflection scenarios such as dynamic method generation or interoperating with unmanaged code.

When dealing with generic methods, use the overload that takes a RuntimeTypeHandle for the declaring type to ensure the correct generic context is applied.

Example

// Obtain MethodInfo for a non‑generic method using its handle
MethodInfo mi = typeof(string).GetMethod("Substring", new[] { typeof(int) });
RuntimeMethodHandle handle = mi.MethodHandle;

MethodBase mb = MethodBase.GetMethodFromHandle(handle);
Console.WriteLine($"Method: {mb.Name}, Declaring type: {mb.DeclaringType}");

/* Generic method example */
MethodInfo genericMi = typeof(List<>).GetMethod("Add");
RuntimeMethodHandle genericHandle = genericMi.MethodHandle;
RuntimeTypeHandle declaring = typeof(List<int>).TypeHandle;

MethodBase genericMb = MethodBase.GetMethodFromHandle(genericHandle, declaring);
Console.WriteLine($"Generic method: {genericMb.Name}, Declaring type: {genericMb.DeclaringType}");


        

See also