/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/MemberListBinding.cs
C# | 141 lines | 69 code | 13 blank | 59 comment | 4 complexity | 274d55bd13cba3d3bf12f372e6f7dff6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense
1/* ****************************************************************************
2 *
3 * Copyright (c) Microsoft Corporation.
4 *
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.
10 *
11 * You must not remove this notice, or any other, from this software.
12 *
13 *
14 * ***************************************************************************/
15
16using System;
17using System.Collections;
18using System.Collections.Generic;
19using System.Collections.ObjectModel;
20using System.Dynamic.Utils;
21using System.Reflection;
22
23#if SILVERLIGHT
24using System.Core;
25#endif
26
27#if CLR2
28namespace Microsoft.Scripting.Ast {
29#else
30namespace System.Linq.Expressions {
31#endif
32
33 /// <summary>
34 /// Represents initializing the elements of a collection member of a newly created object.
35 /// </summary>
36 public sealed class MemberListBinding : MemberBinding {
37 ReadOnlyCollection<ElementInit> _initializers;
38 internal MemberListBinding(MemberInfo member, ReadOnlyCollection<ElementInit> initializers)
39#pragma warning disable 618
40 : base(MemberBindingType.ListBinding, member) {
41#pragma warning restore 618
42 _initializers = initializers;
43 }
44
45 /// <summary>
46 /// Gets the element initializers for initializing a collection member of a newly created object.
47 /// </summary>
48 public ReadOnlyCollection<ElementInit> Initializers {
49 get { return _initializers; }
50 }
51
52 /// <summary>
53 /// Creates a new expression that is like this one, but using the
54 /// supplied children. If all of the children are the same, it will
55 /// return this expression.
56 /// </summary>
57 /// <param name="initializers">The <see cref="Initializers" /> property of the result.</param>
58 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
59 public MemberListBinding Update(IEnumerable<ElementInit> initializers) {
60 if (initializers == Initializers) {
61 return this;
62 }
63 return Expression.ListBind(Member, initializers);
64 }
65 }
66
67
68 public partial class Expression {
69
70 ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> where the member is a field or property.</summary>
71 ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" /> and the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> properties set to the specified values.</returns>
72 ///<param name="member">A <see cref="T:System.Reflection.MemberInfo" /> that represents a field or property to set the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property equal to.</param>
73 ///<param name="initializers">An array of <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
74 ///<exception cref="T:System.ArgumentNullException">
75 ///<paramref name="member" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
76 ///<exception cref="T:System.ArgumentException">
77 ///<paramref name="member" /> does not represent a field or property.-or-The <see cref="P:System.Reflection.FieldInfo.FieldType" /> or <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the field or property that <paramref name="member" /> represents does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
78 public static MemberListBinding ListBind(MemberInfo member, params ElementInit[] initializers) {
79 ContractUtils.RequiresNotNull(member, "member");
80 ContractUtils.RequiresNotNull(initializers, "initializers");
81 return ListBind(member, (IEnumerable<ElementInit>)initializers);
82 }
83
84 ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> where the member is a field or property.</summary>
85 ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" /> and the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> properties set to the specified values.</returns>
86 ///<param name="member">A <see cref="T:System.Reflection.MemberInfo" /> that represents a field or property to set the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property equal to.</param>
87 ///<param name="initializers">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
88 ///<exception cref="T:System.ArgumentNullException">
89 ///<paramref name="member" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
90 ///<exception cref="T:System.ArgumentException">
91 ///<paramref name="member" /> does not represent a field or property.-or-The <see cref="P:System.Reflection.FieldInfo.FieldType" /> or <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the field or property that <paramref name="member" /> represents does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
92 public static MemberListBinding ListBind(MemberInfo member, IEnumerable<ElementInit> initializers) {
93 ContractUtils.RequiresNotNull(member, "member");
94 ContractUtils.RequiresNotNull(initializers, "initializers");
95 Type memberType;
96 ValidateGettableFieldOrPropertyMember(member, out memberType);
97 var initList = initializers.ToReadOnly();
98 ValidateListInitArgs(memberType, initList);
99 return new MemberListBinding(member, initList);
100 }
101
102 ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> object based on a specified property accessor method.</summary>
103 ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" />, the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property set to the <see cref="T:System.Reflection.MemberInfo" /> that represents the property accessed in <paramref name="propertyAccessor" />, and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> populated with the elements of <paramref name="initializers" />.</returns>
104 ///<param name="propertyAccessor">A <see cref="T:System.Reflection.MethodInfo" /> that represents a property accessor method.</param>
105 ///<param name="initializers">An array of <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
106 ///<exception cref="T:System.ArgumentNullException">
107 ///<paramref name="propertyAccessor" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
108 ///<exception cref="T:System.ArgumentException">
109 ///<paramref name="propertyAccessor" /> does not represent a property accessor method.-or-The <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the property that the method represented by <paramref name="propertyAccessor" /> accesses does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
110 public static MemberListBinding ListBind(MethodInfo propertyAccessor, params ElementInit[] initializers) {
111 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
112 ContractUtils.RequiresNotNull(initializers, "initializers");
113 return ListBind(propertyAccessor, (IEnumerable<ElementInit>)initializers);
114 }
115
116 ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> based on a specified property accessor method.</summary>
117 ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" />, the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property set to the <see cref="T:System.Reflection.MemberInfo" /> that represents the property accessed in <paramref name="propertyAccessor" />, and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> populated with the elements of <paramref name="initializers" />.</returns>
118 ///<param name="propertyAccessor">A <see cref="T:System.Reflection.MethodInfo" /> that represents a property accessor method.</param>
119 ///<param name="initializers">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
120 ///<exception cref="T:System.ArgumentNullException">
121 ///<paramref name="propertyAccessor" /> is null. -or-One or more elements of <paramref name="initializers" /> are null.</exception>
122 ///<exception cref="T:System.ArgumentException">
123 ///<paramref name="propertyAccessor" /> does not represent a property accessor method.-or-The <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the property that the method represented by <paramref name="propertyAccessor" /> accesses does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
124 public static MemberListBinding ListBind(MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers) {
125 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
126 ContractUtils.RequiresNotNull(initializers, "initializers");
127 return ListBind(GetProperty(propertyAccessor), initializers);
128 }
129
130 private static void ValidateListInitArgs(Type listType, ReadOnlyCollection<ElementInit> initializers) {
131 if (!typeof(IEnumerable).IsAssignableFrom(listType)) {
132 throw Error.TypeNotIEnumerable(listType);
133 }
134 for (int i = 0, n = initializers.Count; i < n; i++) {
135 ElementInit element = initializers[i];
136 ContractUtils.RequiresNotNull(element, "initializers");
137 ValidateCallInstanceType(listType, element.AddMethod);
138 }
139 }
140 }
141}