3

We make use of a custom value type that implements IConvertible to be able to cast it as Double dynamically. We have prototypes that return Object and are susceptible to returning an instance of this type, on which we call CDbl.

In .net Framework, this works fine. In .net5 this works fine. In a netstandard library, this throws an InvalidCastException. Looking at the code, it seems the compiler embeds some parts of code in a local Microsoft.VisualBasic.CompilerServices namespace, in our case it embeds a Conversions class that provides a ToDouble method. However, the code embeded in this case is not the same as the code in the visual basic library. It makes no provisions for IConvertible and just fails.

I have logged an issue on the roslyn repo, but it has stayed without answers so far. Has anyone noticed this and maybe found a workaround? In our case, switching all libraries to double targeting .net4.8 and net50 is not really an easy task, sdue to the architecture of our system, so I would rather not do it...

Denis Troller
  • 7,411
  • 1
  • 23
  • 36
  • 1
    This is really interesting (to me at least); it *sounds* like it is fundamentally a compiler/build thing, so: there probably isn't an immediate fix, even if the code was changed today (which it probably won't be) - being pragmatic, it sounds like you're *either* going to not use `CDbl` (but instead: write the code that you actually need), *or*: you're going to need to multi-target net48 and net5.0 - probably neither of which is what you wanted to hear. – Marc Gravell Nov 19 '20 at 08:25
  • Yeah, I know... I have to either forgo a basic feature of VisualBasic, which is an idiom used throughout any VB project (calling CDbl, which becomes suspicious) or rework my build setup to account for differing versions of the library (I admit this one is entirely our problem). I just think it's important to be aware of the change in behavior. VB might be a zombien but a lot of code still exist than will need to be ported and such a breaking change might come back and bite someone in the rear end at runtime. never a good prospect. – Denis Troller Nov 19 '20 at 08:32

0 Answers0