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.Reflection;
21 using System.Dynamic.Utils;
24 namespace Microsoft.Scripting.Ast {
26 namespace System.Linq.Expressions {
29 /// Represents the initialization of a list.
31 public sealed class ElementInit : IArgumentProvider {
32 private MethodInfo _addMethod;
33 private ReadOnlyCollection<Expression> _arguments;
35 internal ElementInit(MethodInfo addMethod, ReadOnlyCollection<Expression> arguments) {
36 _addMethod = addMethod;
37 _arguments = arguments;
40 /// Gets the <see cref="MethodInfo"/> used to add elements to the object.
42 public MethodInfo AddMethod {
43 get { return _addMethod; }
47 /// Gets the list of elements to be added to the object.
49 public ReadOnlyCollection<Expression> Arguments {
50 get { return _arguments; }
53 Expression IArgumentProvider.GetArgument(int index) {
54 return _arguments[index];
57 int IArgumentProvider.ArgumentCount {
59 return _arguments.Count;
64 /// Creates a <see cref="String"/> representation of the node.
66 /// <returns>A <see cref="String"/> representation of the node.</returns>
67 public override string ToString() {
68 return ExpressionStringBuilder.ElementInitBindingToString(this);
72 /// Creates a new expression that is like this one, but using the
73 /// supplied children. If all of the children are the same, it will
74 /// return this expression.
76 /// <param name="arguments">The <see cref="Arguments" /> property of the result.</param>
77 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
78 public ElementInit Update(IEnumerable<Expression> arguments) {
79 if (arguments == Arguments) {
82 return Expression.ElementInit(AddMethod, arguments);
87 public partial class Expression {
89 /// Creates an <see cref="T:ElementInit">ElementInit</see> expression that represents the initialization of a list.
91 /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
92 /// <param name="arguments">An array containing the Expressions to be used to initialize the list.</param>
93 /// <returns>The created <see cref="T:ElementInit">ElementInit</see> expression.</returns>
94 public static ElementInit ElementInit(MethodInfo addMethod, params Expression[] arguments) {
95 return ElementInit(addMethod, arguments as IEnumerable<Expression>);
99 /// Creates an <see cref="T:ElementInit">ElementInit</see> expression that represents the initialization of a list.
101 /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
102 /// <param name="arguments">An <see cref="IEnumerable{T}"/> containing <see cref="Expression"/> elements to initialize the list.</param>
103 /// <returns>The created <see cref="T:ElementInit">ElementInit</see> expression.</returns>
104 public static ElementInit ElementInit(MethodInfo addMethod, IEnumerable<Expression> arguments) {
105 ContractUtils.RequiresNotNull(addMethod, "addMethod");
106 ContractUtils.RequiresNotNull(arguments, "arguments");
108 var argumentsRO = arguments.ToReadOnly();
110 RequiresCanRead(argumentsRO, "arguments");
111 ValidateElementInitAddMethodInfo(addMethod);
112 ValidateArgumentTypes(addMethod, ExpressionType.Call, ref argumentsRO);
113 return new ElementInit(addMethod, argumentsRO);
116 private static void ValidateElementInitAddMethodInfo(MethodInfo addMethod) {
117 ValidateMethodInfo(addMethod);
118 ParameterInfo[] pis = addMethod.GetParametersCached();
119 if (pis.Length == 0) {
120 throw Error.ElementInitializerMethodWithZeroArgs();
122 if (!addMethod.Name.Equals("Add", StringComparison.OrdinalIgnoreCase)) {
123 throw Error.ElementInitializerMethodNotAdd();
125 if (addMethod.IsStatic) {
126 throw Error.ElementInitializerMethodStatic();
128 foreach (ParameterInfo pi in pis) {
129 if (pi.ParameterType.IsByRef) {
130 throw Error.ElementInitializerMethodNoRefOutParam(pi.Name, addMethod.Name);