Initializing a class like this:
var x = new Item()
{
ID = (int)...,
Name = (string)...,
..
};
I am getting an InvalidCastException on one of the assignments. There are quite a lot of them and the exception occurs on the whole expression even if I run the debugger line-by-line. The exception doesn't give any clue either what it's trying to cast to what.
Is there a way to debug each assignment individually? I've seen the debugger separately stops 3 times on expressions like foreach(x in y)
so it seems a little strange it isn't doing that here, and detracts from the attraction of using this handy initialization syntax. Maybe there is a more fine-grained debug step I can use?
Your question is "Is it possible to debug a struct/class initialization member by member?".
So, up front, I'm not directly answering that question as worded because when I carefully read the body of your post it sounds like the essential question is how to identify the 'smoking line' root cause of this InvalidCastException right when it happens.
What I've found in similar situations is that if Visual Studio can be made to break at the moment the InvalidCastException occurs i.e. on that specific line then the Call Stack and local variables are much more immediate and useful.
Unfortunately, 'break when thrown' is suppressed by Visual Studio default settings for many exception types. But it's very easy to turn on 'break when thrown' for ALL exceptions. Just change this default setting in the Exceptions window in Visual Studio from this:
to this:
This doesn't "always" help but it's a good start. It's so easy why not try that first to see if rapid resolution is possible. Hope this turns out to be useful in your case.
Not sure if this is an option in VS 2017, I only have 2019 at hand. In your settings under Options -> Debugging -> General, uncheck Step over properties and operators. Then set a break point at your initializer and step through it with F11 (Step-Into). You will hit each property setter until the exception is thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With