#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 System.Runtime.CompilerServices;
using LinqInternal.Collections;
namespace System.Linq.Expressions.Reimplement
{
///
/// An expression that provides runtime read/write access to variables.
/// Needed to implement "eval" in some dynamic languages.
/// Evaluates to an instance of when executed.
///
[DebuggerTypeProxy(typeof(RuntimeVariablesExpressionProxy))]
public sealed class RuntimeVariablesExpression : Expression
{
private readonly ReadOnlyCollection _variables;
internal RuntimeVariablesExpression(ReadOnlyCollection variables)
{
_variables = variables;
}
///
/// Gets the static type of the expression that this represents.
///
/// The that represents the static type of the expression.
public override Type Type
{
get { return typeof(IRuntimeVariables); }
}
///
/// Returns the node type of this Expression. Extension nodes should return
/// ExpressionType.Extension when overriding this method.
///
/// The of the expression.
public override ExpressionType NodeType
{
get { return ExpressionType.RuntimeVariables; }
}
///
/// The variables or parameters to which to provide runtime access.
///
public ReadOnlyCollection Variables
{
get { return _variables; }
}
protected internal override Expression Accept(ExpressionVisitor visitor)
{
return visitor.VisitRuntimeVariables(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.
/// This expression if no children changed, or an expression with the updated children.
public RuntimeVariablesExpression Update(IEnumerable variables)
{
if (variables == Variables)
{
return this;
}
return RuntimeVariables(variables);
}
}
public partial class Expression
{
///
/// Creates an instance of .
///
/// An array of objects to use to populate the collection.
/// An instance of that has the property equal to and the property set to the specified value.
public static RuntimeVariablesExpression RuntimeVariables(params ParameterExpression[] variables)
{
return RuntimeVariables((IEnumerable)variables);
}
///
/// Creates an instance of .
///
/// A collection of objects to use to populate the collection.
/// An instance of that has the property equal to and the property set to the specified value.
public static RuntimeVariablesExpression RuntimeVariables(IEnumerable variables)
{
ContractUtils.RequiresNotNull(variables, "variables");
var vars = variables.ToReadOnly();
for (var i = 0; i < vars.Count; i++)
{
Expression v = vars[i];
if (v == null)
{
throw new ArgumentNullException("variables[" + i + "]");
}
}
return new RuntimeVariablesExpression(vars);
}
}
}
#endif