rc1
rc1

Reputation:

VS2008 C# Exceptions with methods invoked by reflection

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

Upvotes: 2

Views: 1146

Answers (3)

TcKs
TcKs

Reputation: 26632

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.

Upvotes: 1

Roger Lipscombe
Roger Lipscombe

Reputation: 91855

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.

Upvotes: 3

Glenner003
Glenner003

Reputation: 1552

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

Upvotes: 1

Related Questions