I want to get IL instruction offset when exception happens (from the start of the method).
For example I have object initializer:
var obj = new SomeObject {
F1 = something.A,
F2 = something.B,
F3 = something.C,
F4 = something2.A,
F5 = something.D
};
If something2
will be null
- exception will get thrown.
Exception stack trace will be pointed at first line:
var obj = new SomeObject {
Can I get exact IL code offset to the problematic object?
In this example I'd like to get offset to this instruction:
callvirt instance void SomeClass2::get_A(class SomeClass2)
Update: answer posted here (via PDB) not gonna work in this situation as line will point exactly at the same place.
If you break on exception, you might be able to create a StackTrace object in the watch window, passing the exception into its constructor and calling GetILOffset()
on the first frame (something like new StackTrace($exception).GetFrame(0).GetILOffset()
IIRC).
Having said that, the JIT may end up mapping all the generated native code for that statement to the same IL offset, in which case there isn't much you can do except to break up the statement.