using System.Collections.Generic;
using System.Linq.Expressions;
using static GetMethodInfoUtil;
using static GetIndirectMethodInfoUtil;
public static void Main(string[] args)
GetMethodInfo<int, int>(Math.Abs),
GetMethodInfo<double, double>(Math.Abs),
GetMethodInfo<long, long, long>(Math.Max),
GetMethodInfo(Console.Clear),
GetMethodInfo<string[]>(Main),
GetMethodInfo((Action<string, object, object>)Console.WriteLine),
GetMethodInfo<string, bool>("".StartsWith),
GetMethodInfo(new List<int>().Clear),
var moreMethodInfos = new[]
GetIndirectMethodInfo(() => "".StartsWith("")),
GetIndirectMethodInfo((string s) => s.StartsWith(s)),
GetIndirectMethodInfo(() => int.TryParse("", out dummyInt)),
foreach(var methodInfo in methodInfos.Concat(moreMethodInfos))
Console.WriteLine(methodInfo);
public static class GetMethodInfoUtil
public static MethodInfo GetMethodInfo(Action action) => action.Method;
public static MethodInfo GetMethodInfo<T>(Action<T> action) => action.Method;
public static MethodInfo GetMethodInfo<T,U>(Action<T,U> action) => action.Method;
public static MethodInfo GetMethodInfo<TResult>(Func<TResult> fun) => fun.Method;
public static MethodInfo GetMethodInfo<T, TResult>(Func<T, TResult> fun) => fun.Method;
public static MethodInfo GetMethodInfo<T, U, TResult>(Func<T, U, TResult> fun) => fun.Method;
public static MethodInfo GetMethodInfo(Delegate del) => del.Method;
public static class GetIndirectMethodInfoUtil
public static MethodInfo GetIndirectMethodInfo(Expression<Action> expression)
=> GetIndirectMethodInfo((LambdaExpression)expression);
public static MethodInfo GetIndirectMethodInfo<T>(Expression<Action<T>> expression)
=> GetIndirectMethodInfo((LambdaExpression)expression);
public static MethodInfo GetIndirectMethodInfo<T, TResult>(Expression<Func<TResult>> expression)
=> GetIndirectMethodInfo((LambdaExpression)expression);
public static MethodInfo GetIndirectMethodInfo<T, TResult>(Expression<Func<T, TResult>> expression)
=> GetIndirectMethodInfo((LambdaExpression)expression);
private static MethodInfo GetIndirectMethodInfo(LambdaExpression expression)
if (!(expression.Body is MethodCallExpression methodCall))
throw new ArgumentException(
$"Invalid Expression ({expression.Body}). Expression should consist of a method call only.");
return methodCall.Method;