Bug 3690 - PropertyInfo.GetValue should wrap the exception in a TargetInvocationException
Summary: PropertyInfo.GetValue should wrap the exception in a TargetInvocationException
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-02-29 15:48 UTC by Sam
Modified: 2012-02-29 17:53 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Sam 2012-02-29 15:48:23 UTC
If the target method of a MethodInfo throws an exception when calling MethodInfo.Invoke, an exception of type TargetInvocationException will be thrown that wraps the original exception in the InnerException property. For example:

public static class MainClass
{
    public static void Main(string[] args)
    {
        var instance = new TestClass();
        var method = typeof(TestClass).GetMethod("ThrowingMethod");

        try
        {
            method.Invoke(instance, null);
        }
        catch (TargetInvocationException ex)
        {
            Console.WriteLine(ex.InnerException.GetType() == typeof(ObjectDisposedException));
        }
    }
}

public sealed class TestClass
{
    public void ThrowingMethod()
    {
        throw new ObjectDisposedException("TestClass");
    }
}


Under Microsoft's .NET, PropertyInfo.GetValue also exhibits this behaviour, however, under Mono 2.10.8 (running on OS X 10.6), the original error is thrown, not a TargetInvocationException. Continuing from the last example:

public static class MainClass
{
    public static void Main(string[] args)
    {
        var instance = new TestClass();
        var property = typeof(TestClass).GetProperty("ThrowingProperty");

        try
        {
            // This throws ObjectDisposedException but should be
            // TargetInvocationException
            property.GetValue(instance, null);
        }
        catch (TargetInvocationException ex)
        {
            // Never executed
            Console.WriteLine(ex.InnerException.GetType() == typeof(ObjectDisposedException));
        }
    }
}

public sealed class TestClass
{
    public int ThrowingProperty
    {
        get
        {
            throw new ObjectDisposedException("TestClass");
        }
    }
}
Comment 1 Zoltan Varga 2012-02-29 17:53:36 UTC
Fixed in master/2.10 branch.