#if NET20 || NET30 || !NET_4_6
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using LinqInternal.Collections;
namespace System.Linq.Expressions.Reimplement
{
///
/// Represents one case of a .
///
[DebuggerTypeProxy(typeof(SwitchCaseProxy))]
public sealed class SwitchCase
{
private readonly ReadOnlyCollection _testValues;
private readonly Expression _body;
internal SwitchCase(Expression body, ReadOnlyCollection testValues)
{
_body = body;
_testValues = testValues;
}
///
/// Gets the values of this case. This case is selected for execution when the matches any of these values.
///
public ReadOnlyCollection TestValues
{
get { return _testValues; }
}
///
/// Gets the body of this case.
///
public Expression Body
{
get { return _body; }
}
///
/// Returns a that represents the current .
///
/// A that represents the current .
public override string ToString()
{
return ExpressionStringBuilder.SwitchCaseToString(this);
}
///
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
///
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public SwitchCase Update(IEnumerable testValues, Expression body)
{
if (testValues == TestValues && body == Body)
{
return this;
}
return Expression.SwitchCase(body, testValues);
}
}
public partial class Expression
{
///
/// Creates a SwitchCase for use in a .
///
/// The body of the case.
/// The test values of the case.
/// The created SwitchCase.
public static SwitchCase SwitchCase(Expression body, params Expression[] testValues)
{
return SwitchCase(body, (IEnumerable)testValues);
}
///
/// Creates a SwitchCase for use in a .
///
/// The body of the case.
/// The test values of the case.
/// The created SwitchCase.
public static SwitchCase SwitchCase(Expression body, IEnumerable testValues)
{
RequiresCanRead(body, "body");
var values = testValues.ToReadOnly();
RequiresCanRead(values, "testValues");
ContractUtils.RequiresNotEmpty(values, "testValues");
return new SwitchCase(body, values);
}
}
}
#endif