1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.Diagnostics;
22 using System.Dynamic.Utils;
24 using Microsoft.Scripting.Utils;
28 namespace System.Linq.Expressions {
30 namespace Microsoft.Linq.Expressions {
33 /// Represents one case of a <see cref="SwitchExpression"/>.
36 [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
38 public sealed class SwitchCase {
39 private readonly ReadOnlyCollection<Expression> _testValues;
40 private readonly Expression _body;
42 internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
44 _testValues = testValues;
48 /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
50 public ReadOnlyCollection<Expression> TestValues {
51 get { return _testValues; }
55 /// Gets the body of this case.
57 public Expression Body {
62 /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>.
64 /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
65 public override string ToString() {
66 return ExpressionStringBuilder.SwitchCaseToString(this);
70 public partial class Expression {
72 /// Creates a <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
74 /// <param name="body">The body of the case.</param>
75 /// <param name="testValues">The test values of the case.</param>
76 /// <returns>The created <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see>.</returns>
77 public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
78 return SwitchCase(body, (IEnumerable<Expression>)testValues);
82 /// Creates a <see cref="Microsoft.Linq.Expressions.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="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see>.</returns>
87 public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
88 RequiresCanRead(body, "body");
90 var values = testValues.ToReadOnly();
91 RequiresCanRead(values, "testValues");
92 ContractUtils.RequiresNotEmpty(values, "testValues");
94 return new SwitchCase(body, values);