Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't decompile to switch-expression when value is implicitly cast #2222

Closed
Pathoschild opened this issue Nov 16, 2020 · 4 comments
Closed
Labels

Comments

@Pathoschild
Copy link
Contributor

Pathoschild commented Nov 16, 2020

ILSpy can't decompile a switch statement when the source value is implicitly cast. This broke in ILSpy 6.2+.

Steps to reproduce

  1. Unzip the repro DLL, which contains this code:
    public class SampleClass
    {
        public string SampleMethod()
        {
            string value = new ImplicitlyCastToString();
            return value switch
            {
                "foo" => "foo",
                "bar" => "bar",
                "quux" => "quux",
                _ => "default"
            };
        }
    }
    
    public class ImplicitlyCastToString
    {
        public static implicit operator string(ImplicitlyCastToString val)
        {
            return "foo";
        }
    }
  2. Decompile the .dll file with ILSpy.

Resulting code:

ILSpy 6.1 (working) ILSpy 6.2+ (broken)
return (string)new ImplicitlyCastToString() switch
{
    "foo" => "foo", 
    "bar" => "bar", 
    "quux" => "quux", 
    _ => "default", 
};
return new ImplicitlyCastToString() switch
{
    0L => "foo", 
    1L => "bar", 
    2L => "quux", 
    _ => "default", 
};

Details

  • Product in use: ILSpy
  • Version in use:
    tested version result
    6.1.0.5902 ✓ ok
    6.2.0.6062-preview1 ✓ ok
    6.2.0.6118-preview2 ✘ fails
    6.2.0.6124 ✘ fails
    6.2.1.6137 ✘ fails
    7.0.0.6225-preview1 ✘ fails
@CreateAndInject
Copy link
Contributor

CreateAndInject commented Nov 17, 2020

@Pathoschild I'm sorry, I just want to know how to use markdown to generate this table with multi content:

image

This code can't work:

image

@Pathoschild
Copy link
Contributor Author

@CreateAndInject GitHub allows some basic HTML for cases like this; here's the code for that table.

@CreateAndInject
Copy link
Contributor

@Pathoschild Thanks, I have collapsed my content.

@siegfriedpammer
Copy link
Member

@siegfriedpammer siegfriedpammer changed the title Can't decompile switch when source value is implicitly cast Can't decompile to switch-expression when value is implicitly cast Nov 22, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2021
ElektroKill pushed a commit to dnSpyEx/ILSpy that referenced this issue Aug 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants