A C# Bug in LINQPad Beta v8.4.1
Please check out my query: https://share.linqpad.net/x7nnapuq.linq
In Line 164, it will fail:
// This will fail to run. var type1 = value.GetType(); var type = Type.GetTypeCode(type1); <-- RuntimeBinderException switch (type) {}
Member 'System.Type.GetTypeCode(System.Type)' cannot be accessed with an instance reference; qualify it with a type name instead
Screenshot:
It's can't be run in LINQPad, but it works in a normal .NET Console app. I don't know why.
Comments
The problem specifically is that the variable
value
is typeddynamic
so you've kinda polluted all interactions with that variable. You should cast it toobject
prior to working with it.Generally speaking, I don't think you should be using
dynamic
for a type parameter for this reason. The implementations could work with the object and treat it as if it was dynamic, but should never be declared dynamic. You're just going to introduce a host of problems down the line.As for why it behaves differently in LINQPad, I suppose it's how LINQPad transforms the queries to have a completely different structure than how it is normally in a C# file since LINQPad allows us to mix UserQuery instance members with top-level classes (like with extension classes must be top-level so LINQPad has to rearrange things).
The error occurs in .NET 8 but not in .NET 7.
Is it possible that you've chosen .NET 8 in LINQPad, and .NET 7 in your Console app?