xzeed
xzeed

Reputation: 45

MethodInfo.Invoke throws unexpected exception

Im trying to make a custom shell.

public static void CallMethod(string method, string[] args)
    {
        try
        {
            Type type = typeof(Program);
            MethodInfo methodInfo = type.GetMethod(method);
            methodInfo.Invoke(method, args);
        }
        catch(TargetException)
        {
            Console.WriteLine("The method + '" + method + "' does not exist.");
        }
        catch(TargetParameterCountException)
        {
            Console.WriteLine("The parameter count for '" + method +"' does not fit.");
        }
        catch(Exception)
        {
            Console.WriteLine("An unexpected error occured.");
        }
    }

I am expecting TargetException to be thrown when the method-string doesnt contain an existing method - like the MS documentation says. But in this case the Exception is thrown. Little example: There is no function example(), so it should throw TargetException when method contains "example", but it seems that another Exception is thrown

My question ist what exception is thrown in this specific case?

Upvotes: 0

Views: 278

Answers (0)

Related Questions