diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs index 1b4de4422..f49d3ea68 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs @@ -411,7 +411,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public static string SwitchOverImplicitString(ImplicitString s) { - switch (s) + // we emit an explicit cast, because the rules used by the C# compiler are counter-intuitive: + // The C# compiler does *not* take the type of the switch labels into account at all. + switch ((string)s) { case "First case": return "Text1"; diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index bd9cbe5ed..a7db95ae8 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -205,7 +205,9 @@ namespace ICSharpCode.Decompiler.CSharp .ConvertTo( typeSystem.FindType(KnownTypeCode.String), exprBuilder, - allowImplicitConversion: true + // switch statement does support implicit conversions in general, however, the rules are + // not very intuitive and in order to prevent bugs, we emit an explicit cast. + allowImplicitConversion: false ); type = exprBuilder.compilation.FindType(KnownTypeCode.String); }