Last active
July 12, 2021 15:21
-
-
Save DrewQuick/9c3f7eaa21fe7a7d91442efcb4c33afa to your computer and use it in GitHub Desktop.
Dynamic Invocation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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