Skip to content

Instantly share code, notes, and snippets.

@DrewQuick
Last active July 12, 2021 15:21
Show Gist options
  • Save DrewQuick/9c3f7eaa21fe7a7d91442efcb4c33afa to your computer and use it in GitHub Desktop.
Save DrewQuick/9c3f7eaa21fe7a7d91442efcb4c33afa to your computer and use it in GitHub Desktop.
Dynamic Invocation
public static void ExecuteMethod(string MethodName, Type ClassType = null)
{
if (string.IsNullOrWhiteSpace(MethodName))
throw new Exception("First argument must contain the target method name.");
if (ClassType == null)
ClassType = new StackFrame().GetMethod().DeclaringType;
var methodInfo = ClassType.GetMethods().Where(x => x.Name.ToLower() == MethodName.ToLower()).FirstOrDefault();
if (methodInfo == null)
throw new Exception($"Unable to retrieve target method: {MethodName}().");
methodInfo.Invoke(typeof(Program), null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment