DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.1.2" />

 MethodExtensions

public static class MethodExtensions
Various extension methods for method reflection.
using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; namespace DotNext.Reflection { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class MethodExtensions { [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] [return: NotNullIfNotNull("method")] public static Type[] GetParameterTypes(this MethodBase method) { if ((object)method != null) return Array.ConvertAll(method.GetParameters(), (ParameterInfo p) => p.ParameterType); return null; } public static bool SignatureEquals(this MethodBase method, Type[] parameters) { Type[] parameterTypes = method.GetParameterTypes(); if (parameterTypes.LongLength != parameters.LongLength) return false; for (long num = 0; num < parameterTypes.LongLength; num++) { if (parameterTypes[num] != parameters[num]) return false; } return true; } public static bool SignatureEquals(this MethodBase method, MethodBase other, bool respectCallingConvention = false) { if (!respectCallingConvention || method.CallingConvention == other.CallingConvention) return method.SignatureEquals(other.GetParameterTypes()); return false; } public static bool SignatureEquals(this MethodInfo method, MethodInfo other, bool respectCallingConvention = false) { if (((MethodBase)method).As().SignatureEquals(other, respectCallingConvention)) return method.ReturnType == other.ReturnType; return false; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static Result<object> TryInvoke(this MethodBase method, [System.Runtime.CompilerServices.Nullable(2)] object obj, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { try { return method.Invoke(obj, args); } catch (Exception error) { return new Result<object>(error); } } } }