1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Diagnostics;
20 using System.Dynamic.Utils;
23 namespace Microsoft.Scripting.Ast {
25 namespace System.Linq.Expressions {
28 /// Represents one case of a <see cref="SwitchExpression"/>.
31 [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
33 public sealed class SwitchCase {
34 private readonly ReadOnlyCollection<Expression> _testValues;
35 private readonly Expression _body;
37 internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
39 _testValues = testValues;
43 /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
45 public ReadOnlyCollection<Expression> TestValues {
46 get { return _testValues; }
50 /// Gets the body of this case.
52 public Expression Body {
57 /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>.
59 /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
60 public override string ToString() {
61 return ExpressionStringBuilder.SwitchCaseToString(this);
65 /// Creates a new expression that is like this one, but using the
66 /// supplied children. If all of the children are the same, it will
67 /// return this expression.
69 /// <param name="testValues">The <see cref="TestValues" /> property of the result.</param>
70 /// <param name="body">The <see cref="Body" /> property of the result.</param>
71 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
72 public SwitchCase Update(IEnumerable<Expression> testValues, Expression body) {
73 if (testValues == TestValues && body == Body) {
76 return Expression.SwitchCase(body, testValues);
80 public partial class Expression {
82 /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
84 /// <param name="body">The body of the case.</param>
85 /// <param name="testValues">The test values of the case.</param>
86 /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
87 public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
88 return SwitchCase(body, (IEnumerable<Expression>)testValues);
92 /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
94 /// <param name="body">The body of the case.</param>
95 /// <param name="testValues">The test values of the case.</param>
96 /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
97 public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
98 RequiresCanRead(body, "body");
100 var values = testValues.ToReadOnly();
101 RequiresCanRead(values, "testValues");
102 ContractUtils.RequiresNotEmpty(values, "testValues");
104 return new SwitchCase(body, values);