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"/>.
30 [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
31 public sealed class SwitchCase {
32 private readonly ReadOnlyCollection<Expression> _testValues;
33 private readonly Expression _body;
35 internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
37 _testValues = testValues;
41 /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
43 public ReadOnlyCollection<Expression> TestValues {
44 get { return _testValues; }
48 /// Gets the body of this case.
50 public Expression Body {
55 /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>.
57 /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
58 public override string ToString() {
59 return ExpressionStringBuilder.SwitchCaseToString(this);
63 /// Creates a new expression that is like this one, but using the
64 /// supplied children. If all of the children are the same, it will
65 /// return this expression.
67 /// <param name="testValues">The <see cref="TestValues" /> property of the result.</param>
68 /// <param name="body">The <see cref="Body" /> property of the result.</param>
69 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
70 public SwitchCase Update(IEnumerable<Expression> testValues, Expression body) {
71 if (testValues == TestValues && body == Body) {
74 return Expression.SwitchCase(body, testValues);
78 public partial class Expression {
80 /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
82 /// <param name="body">The body of the case.</param>
83 /// <param name="testValues">The test values of the case.</param>
84 /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
85 public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
86 return SwitchCase(body, (IEnumerable<Expression>)testValues);
90 /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
92 /// <param name="body">The body of the case.</param>
93 /// <param name="testValues">The test values of the case.</param>
94 /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
95 public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
96 RequiresCanRead(body, "body");
98 var values = testValues.ToReadOnly();
99 RequiresCanRead(values, "testValues");
100 ContractUtils.RequiresNotEmpty(values, "testValues");
102 return new SwitchCase(body, values);