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 * ***************************************************************************/
19 using System; using Microsoft;
21 using System.Collections.Generic;
22 using System.Collections.ObjectModel;
23 using System.Reflection;
26 using System.Dynamic.Utils;
28 using Microsoft.Scripting.Utils;
32 namespace System.Linq.Expressions {
34 namespace Microsoft.Linq.Expressions {
37 /// Represents the initialization of a list.
39 public sealed class ElementInit : IArgumentProvider {
40 private MethodInfo _addMethod;
41 private ReadOnlyCollection<Expression> _arguments;
43 internal ElementInit(MethodInfo addMethod, ReadOnlyCollection<Expression> arguments) {
44 _addMethod = addMethod;
45 _arguments = arguments;
48 /// Gets the <see cref="MethodInfo"/> used to add elements to the object.
50 public MethodInfo AddMethod {
51 get { return _addMethod; }
55 /// Gets the list of elements to be added to the object.
57 public ReadOnlyCollection<Expression> Arguments {
58 get { return _arguments; }
61 Expression IArgumentProvider.GetArgument(int index) {
62 return _arguments[index];
65 int IArgumentProvider.ArgumentCount {
67 return _arguments.Count;
72 /// Creates a <see cref="String"/> representation of the node.
74 /// <returns>A <see cref="String"/> representation of the node.</returns>
75 public override string ToString() {
76 return ExpressionStringBuilder.ElementInitBindingToString(this);
81 public partial class Expression {
83 /// Creates an <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression that represents the initialization of a list.
85 /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
86 /// <param name="arguments">An array containing the Expressions to be used to initialize the list.</param>
87 /// <returns>The created <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression.</returns>
88 public static ElementInit ElementInit(MethodInfo addMethod, params Expression[] arguments) {
89 return ElementInit(addMethod, arguments as IEnumerable<Expression>);
93 /// Creates an <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression that represents the initialization of a list.
95 /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
96 /// <param name="arguments">An <see cref="IEnumerable{T}"/> containing <see cref="Expression"/> elements to initialize the list.</param>
97 /// <returns>The created <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression.</returns>
98 public static ElementInit ElementInit(MethodInfo addMethod, IEnumerable<Expression> arguments) {
99 ContractUtils.RequiresNotNull(addMethod, "addMethod");
100 ContractUtils.RequiresNotNull(arguments, "arguments");
102 var argumentsRO = arguments.ToReadOnly();
104 RequiresCanRead(argumentsRO, "arguments");
105 ValidateElementInitAddMethodInfo(addMethod);
106 ValidateArgumentTypes(addMethod, ExpressionType.Call, ref argumentsRO);
107 return new ElementInit(addMethod, argumentsRO);
110 private static void ValidateElementInitAddMethodInfo(MethodInfo addMethod) {
111 ValidateMethodInfo(addMethod);
112 ParameterInfo[] pis = addMethod.GetParametersCached();
113 if (pis.Length == 0) {
114 throw Error.ElementInitializerMethodWithZeroArgs();
116 if (!addMethod.Name.Equals("Add", StringComparison.OrdinalIgnoreCase)) {
117 throw Error.ElementInitializerMethodNotAdd();
119 if (addMethod.IsStatic) {
120 throw Error.ElementInitializerMethodStatic();
122 foreach (ParameterInfo pi in pis) {
123 if (pi.ParameterType.IsByRef) {
124 throw Error.ElementInitializerMethodNoRefOutParam(pi.Name, addMethod.Name);