Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Created July 4, 2012 20:50
Show Gist options
  • Select an option

  • Save jeremiahredekop/3049473 to your computer and use it in GitHub Desktop.

Select an option

Save jeremiahredekop/3049473 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremiahredekop created this gist Jul 4, 2012.
    26 changes: 26 additions & 0 deletions GetMethodInfo.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    public MethodInfo GetMethodInfo<TSource>(Expression<Action<TSource>> methodLambda)
    {
    Type type = typeof(TSource);

    var member = methodLambda.Body as MethodCallExpression;
    if (member == null)
    throw new ArgumentException(string.Format(
    "Expression '{0}' does not refer to a method.",
    methodLambda));

    var methodInfo = member.Method as MethodInfo;
    if (methodInfo == null)
    throw new ArgumentException(string.Format(
    "Expression '{0}' does not refer to a method.",
    methodLambda));

    if (type != methodInfo.ReflectedType &&
    !type.IsSubclassOf(methodInfo.ReflectedType))
    throw new ArgumentException(string.Format(
    "Expresion '{0}' refers to a method that is not from type {1}.",
    methodLambda, type));

    return methodInfo;


    }