Uncatcheable exception from MethodInfo.Invoke

前端 未结 4 1799
闹比i
闹比i 2020-12-22 09:08

I have this code which Invokes a MethodInfo:

try
{
     registrator.Method.Invoke(instance, parameters);
}
catch{
    registrator.FailureType = RegistratorFa         


        
4条回答
  •  囚心锁ツ
    2020-12-22 09:42

    The problem is not with the code you're showing.

    I tried this:

    void Main()
    {
        Test instance = new Test();
        object[] parameters = new object[] { null };
    
        MethodInfo method = typeof(Test).GetMethod("Register");
    
        try
        {
            method.Invoke(instance, parameters);
        }
        catch
        {
            Console.Out.WriteLine("Exception");
        }
    }
    
    interface ITest { }
    interface IWindow { }
    class Plugin { }
    
    class Test : Plugin, ITest
    {
        public void Register(IWindow window)
        {
            throw new Exception("Hooah");
        }
    }
    

    It printed "Exception" as expected. You need to show us more code.

    If I modify the code like this:

    catch(Exception ex)
    {
        Console.Out.WriteLine(ex.GetType().Name + ": " + ex.Message);
    }
    

    Then I get a TargetInvocationException, where the InnerException property is your thrown Exception.

提交回复
热议问题