Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:58:45+00:00 2026-05-23T19:58:45+00:00

In our integration project we are dynamically delegating to a series of specific Processor

  • 0

In our integration project we are dynamically delegating to a series of specific Processor objects using Reflection.

var genericType = typeof(IMessageProcessor<>).MakeGenericType(parseResponse.DomainMessage.GetType());

var miProcessMessage = genericType.GetMethod("ProcessMessage");
miProcessMessage.Invoke(processor, new object[] { parseResponse.DomainMessage });

Now, in certain situations, one of the processors needs to throw a specific Exception, in this case when he doesn’t find a file in the expected location.

throw new ResourceNotFoundException(string.Format("Could not find file {0}", filePath));

Our processor wants to check on this exception to determine whether processing needs to be retried or aborted (based on a time-based mechanism, which is not relevant here).

catch(ResourceNotFoundException ex)
{
   // do stuff
}

The problem is that the invoke apparently “swallows” the specific ResourceNotFoundException and rethrows a System.Exception saying “Exception has been thrown by the target of an invocation.”

Is there any way of preserving the fact through Reflection that a ResourceNotFoundException was actually thrown and not an Exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:58:47+00:00Added an answer on May 23, 2026 at 7:58 pm

    MethodBase.Invoke(Object, Object[]) wraps any exception thrown by the invoked method in a TargetInvocationException. You can access the inner exception and either process it directly, or rethrow it to be processed further up the stack as in the following example:

    var genericType = typeof(IMessageProcessor<>).MakeGenericType(parseResponse.DomainMessage.GetType());
    var miProcessMessage = genericType.GetMethod("ProcessMessage");
    try {
        miProcessMessage.Invoke(processor, new object[] { parseResponse.DomainMessage });
    catch (TargetInvocationException ex)
    {
        throw ex.InnerException;
    }
    

    Note however that this will replace the inner exception’s stack trace. A (technically unsupported) way of preserving the stack trace is described in this answer on SO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In using our TeamCity Continuous Integration server we have uncovered some issues that we
We're using CruiseControl.Net to do our continuous integration of our web applications. we build
Our continuous integration system currently runs a perl script to parse our sln/proj files
We use cruise control .net to do our Continuous Integration in our Delphi 2006
I am trying to make our SQL Server Integration Services packages as portable as
Our team is setting up nightly and continuous integration builds. We own Team Foundation
Our marketing department comes back with active directory integration being a key customer request,
We are migrating our test report data (unit, regression, integration, etc..) from an XML
My company is currently using Sage MAS as their ERP system. While integrating our
My organisation has started a continuous integration project to automate the build of their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.