Home
Options

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

  • Options
    edited May 6

    The problem specifically is that the variable value is typed dynamic so you've kinda polluted all interactions with that variable. You should cast it to object 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).

  • Options

    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?

Sign In or Register to comment.