tags:

views:

627

answers:

3

If I invoke a method which does something illegal, the debugger will stop at the line of code, in that method, which threw the exception

If I use reflection to call a method via Invoke and that method throws an exception, the debugger stops on line where the method was called via reflection and not in the faulty method itself

How do I change this, and have the debugger stop on the line of code in error in regardless of how the method was invoked?

The code is built in Debug

+1  A: 

Are you using a debug version of the assembly? If not, the debugger cannot locate the source of the exception.

Glenner003
yes I am using the debug version
+3  A: 

Check the "Thrown" box for the particular exception -- the debugger will stop where the exception is thrown, before it's caught (and translated) by the Invoke layer.

Roger Lipscombe
Of course!! Thanks - that's really really helped :-D
kronoz
+1  A: 

If you know this signature of method called by reflection. You can create a delegate.

If you have the class:

public class MyClass {
    private string GetSomeText() { return DateTime.Now.ToString(); }
}

you can create a delegate:

delegate string DlgGetSomeText();

and then create a delegate's instance with referenco to concrete method:

MyClass cls = new MyClass();
DlgGetSomeText dlg = (DlgGetSomeText)Delegate.CreateDelegate( cls.GetType(), cls, "GetSomeText" );
string result = dlg();

If you will use a delegate, the reflection calling will not be used, so your problem will nod appear.

TcKs
Thank you but it seems quite a bit of work just to have the debugger stop where it should!
Yes I know. But I doesn't develop VS :).The next advantage is the performance. Creating a delegate is much more faster than calling with reflection.
TcKs
Thanks for this answer; very helpful. The use of delegate in this way also eases the passing of parameters -- in code one merely calls the delegate as if a function pointer without needing to create an array of objects for parameters.
Heath Hunnicutt