Namespace: System.Reflection
Assembly: System.Private.CoreLib.dll
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle);
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType);
Name | Type | Description |
---|---|---|
handle | RuntimeMethodHandle | The handle of the method to retrieve. |
declaringType | RuntimeTypeHandle | (Optional) The handle for the type that declares the method. Required for generic methods. |
Returns a MethodBase
instance that represents the method identified by the provided handle.
Exception | Condition |
---|---|
ArgumentNullException | If handle is zero. |
ArgumentException | If the method handle is invalid or the declaring type does not match. |
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.
// 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}");