public override System.Linq.Expressions.Expression Transform() { var transformedExp = Expression.TransformRead(); var transformedCases = Cases.Select(x => x.Transform()).ToArray(); var hiddenVar = System.Linq.Expressions.Expression.Variable(typeof(object), "__switchVariable__"); List <System.Linq.Expressions.Expression> exp = new List <System.Linq.Expressions.Expression>(); exp.Add(System.Linq.Expressions.Expression.Assign(hiddenVar, transformedExp)); var defaultIndex = Cases.FindIndex(x => x.ContainsDefault); var builder = Microsoft.Scripting.Ast.Utils.If(); for (int i = 0; i < transformedCases.Length; i++) { if (transformedCases[i].Item1.Length > 0) { var test = LanguageContext.Convert(BinaryExpression.TransformSimpleOperation(LanguageContext, hiddenVar, transformedCases[i].Item1[0], BinaryOperator.Equal), typeof(bool)); for (int j = 1; j < transformedCases[i].Item1.Length; i++) { test = System.Linq.Expressions.Expression.OrElse(test, LanguageContext.Convert(BinaryExpression.TransformSimpleOperation(LanguageContext, hiddenVar, transformedCases[i].Item1[i], BinaryOperator.Equal), typeof(bool))); } builder.ElseIf(test, System.Linq.Expressions.Expression.Goto(Cases[i].CaseLabel)); } } exp.Add(builder.Else(System.Linq.Expressions.Expression.Goto(defaultIndex >= 0 ? Cases[defaultIndex].CaseLabel : BreakLabel))); for (int i = 0; i < transformedCases.Length; i++) { exp.Add(System.Linq.Expressions.Expression.Label(Cases[i].CaseLabel)); foreach (var transformedStatement in transformedCases[i].Item2) { exp.Add(transformedStatement); } } exp.Add(System.Linq.Expressions.Expression.Label(BreakLabel)); return(System.Linq.Expressions.Expression.Block(new[] { hiddenVar }, exp)); }