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

  • SEARCH
  • Home
  • 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 7660765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:30:35+00:00 2026-05-31T13:30:35+00:00

This is a new attempt to pose a version of a question asked less

  • 0

This is a new attempt to pose a version of a question asked less successfully this morning.

Consider the following program, which we’ll run once inside Visual Studio 2010 and once more by double-clicking the executable directly

namespace ConsoleApplication3
{
    delegate void myFoo(int i, string s);

    class Program
    {
        static void Main(string[] args)
        {
            Foo(1, "hello");
            Delegate Food = (myFoo)Foo;
            Food.DynamicInvoke(new object[] { 2, null });
        }

        static void Foo(int i, string s)
        {
            Console.WriteLine("If the next line triggers an exception, the stack will be unwound up to the .Invoke");
            Console.WriteLine("i=" + i + ", s.Length = " + s.Length);
        }
    }
}

When the exception in Foo triggers while running VS, the debugger shows the stack correctly and shows that the problem occured on the second WriteLine in Foo.

But when the exception occurs while running the executable directly, one gets a little popup window from the CLR indicating that the program threw an unhandled exception. Click debug and select the VS debugger. In this case, the stack unwinds up to the point of the most recent .DynamicInvoke and when you attach with the debugger, the stack context that existed at the time of the exception has been partially lost.

It does exist, in a limited form, within the “inner exception” portion of the exception event. You click to expand the associated information and find the line number where the problem occured. But obviously local variables and other context will be gone.

If one tries the same thing but without the .DynamicInvoke (for example, call Foo(1, null) on line 1 of Main), still by double-clicking the .exe file, we DO get the correct line number when the debugger attaches. Similarly if the application is launched by clicking on the .exe, but then the debugger is attached before the exception gets thrown.

Does anyone know how an application using dynamic reflection/invocation could avoid this problem? In my intended use case, in a system the name of which I won’t mention here, I cannot predict the type signature of the object that will be used in the .DynamicInvoke, or even the number of arguments that will be employed, hence static typing or even generics aren’t a way out of this.

My question is this: does anyone know why we get such different behaviors when running directly from the debugger versus when attaching to the program after the exception is thrown?

  • 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-31T13:30:36+00:00Added an answer on May 31, 2026 at 1:30 pm

    As per the comments, whether you see the NullReferenceException as unhandled depends on whether it’s handled. Here are some ways to call Foo, the first three will leave the exception as unhandled, the last two will handle the NullReferenceException by wrapping it, and throwing a new exception.

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace ConsoleApplication3
    {
        delegate void myFoo(int i, string s);
    
        internal class Program
        {
            private static void Main(string[] args)
            {
                Foo(1, "hello");
    
                // From a delegate
                try
                {
                    Delegate Food = (myFoo)Foo;
                    ((dynamic)Food).Invoke(2, null);
                }
                catch (NullReferenceException ex)
                { Console.WriteLine("Caught NullReferenceException at " + ex.StackTrace); }
    
                MethodInfo Foom = typeof(Program).GetMethod("Foo", BindingFlags.Static | BindingFlags.NonPublic);
    
                // From a MethodInfo, obtaining a delegate from it
                try
                {
                    Delegate Food = Delegate.CreateDelegate(typeof(Action<,>).MakeGenericType(Foom.GetParameters().Select(p => p.ParameterType).ToArray()), Foom);
                    ((dynamic)Food).Invoke(2, null);
                }
                catch (NullReferenceException ex)
                { Console.WriteLine("Caught NullReferenceException at " + ex.StackTrace); }
    
                // From a MethodInfo, creating a plain Action
                try
                {
                    Expression.Lambda<Action>(
                        Expression.Call(
                            Foom,
                            Expression.Constant(2),
                            Expression.Constant(null, typeof(string)))).Compile()();
                }
                catch (NullReferenceException ex)
                { Console.WriteLine("Caught NullReferenceException at " + ex.StackTrace); }
    
                // MethodBase.Invoke, exception gets wrapped
                try
                {
                    Foom.Invoke(null, new object[] { 2, null });
                }
                catch (NullReferenceException)
                { Console.WriteLine("Won't catch NullReferenceException"); }
                catch (TargetInvocationException)
                { Console.WriteLine("Bad!"); }
    
                // DynamicInvoke, exception gets wrapped
                try
                {
                    Delegate Food = (myFoo)Foo;
                    Food.DynamicInvoke(2, null);
                }
                catch (NullReferenceException)
                { Console.WriteLine("Won't catch NullReferenceException"); }
                catch (TargetInvocationException)
                { Console.WriteLine("Bad!"); }
            }
    
            private static void Foo(int i, string s)
            {
                Console.WriteLine("i=" + i + ", s.Length = " + s.Length);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this: require 'Twitter.class.php'; $tweet = new Twitter(username, password); foreach($comment as $key => $value)
I made my first jQuery plugin attempt, but I have this problem/bug which I
I've came across this new acronym, SOFEA, apparently a new programming paradigm for web
Why does this: (new[]{1,2,3}).Cast<decimal>(); result in an InvalidCastException: Specified cast is not valid.
So there's this new cool thing, these NoSQL-databases. And so there's my data: Rows
I keep reading about how great this new Canvas element for HTML5 is and
I just started to read about this new technology... Does someone have some knowledge
Soooo, I am starting this new job soon where most of the code is
Or is it okay to do something like this: new Thread( new ThreadStart( delegate
Many years ago I remember a fellow programmer counselling this: new Some::Class; # bad!

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.