C# exception for LINQ Select() in immediate window, but same code works when compiled

618 Views Asked by At

When I execute these code lines

var lengths = new CPhqLength[]{new CPhqLength(5, EnumUnitLength.Meter)};
lengths.First().ToString(6)

in the immediate window while the execution of my program is interrupted, I get

Expression has been evaluated and has no value

from the first line and

"5,000000 m"

from the second line.

But when I use the LINQ method Select(), an exception is thrown somewhere and it is stored as an object in the returned collection:

?lengths.Select (l => l.ToString (6));
{System.Linq.Enumerable.WhereSelectArrayIterator<Schmoll.SwCore.CPhqLength, string>}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147024809
    HelpLink: null
    InnerException: null
    Message: "mc_dictUnitInfoDictionary:  The enum value Meter is not defined in the enum Schmoll.SwCore.EnumUnitLength."
    ParamName: null
    Source: "System.Core"
    StackTrace: "   at System.Linq.Enumerable.WhereSelectArrayIterator`2[[Schmoll.SwCore.CPhqLength, schmoll_swcore_basics, Version=0.24.9000.0, Culture=neutral, PublicKeyToken=f8a6c8bb21c5d4a1],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()\r\n   at System.Linq.SystemCore_EnumerableDebugView`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_Items()"
    TargetSite: {Boolean MoveNext()}

and I have no chance to debug it in any way. Every breakpoint I set is ignored. No exception settings are respected.
This is a simplified example, but it produces the same failure.
mc_dictUnitInfoDictionary is a static readonly ReadOnlyDictionary, and it definitly is initialized (because it's used) by the program, which is interrupted when executing the failing line in the immediate window. The keys of that dictionary are of System.Type, and

public static CUnitInfo GetInfo<TUnit> (TUnit i_unit)
{
  var enumType = i_unit.GetType ();
  if (!mc_dictUnitInfoDictionary.ContainsKey (enumType))
    throw CException.Create_EnumUndefined (nameof (mc_dictUnitInfoDictionary) + ": ", i_unit, null);

is used to access the keys.
The exception above is an ArgumentException which my code throws if an entry is not found in the dictionary.

How can I find out what is going wrong here?
The same line executes fine if written inside a program.

I am using VS 2019 (v.16.5.5) on Win 10 x64. Compilation for x86 or x64 does not make a difference.

0

There are 0 best solutions below