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.Dynamic.Utils;
20 using System.Reflection;
27 namespace Microsoft.Scripting.Ast {
29 namespace System.Linq.Expressions {
32 /// Represents initializing members of a member of a newly created object.
35 /// Use the <see cref="M:MemberBind"/> factory methods to create a <see cref="MemberMemberBinding"/>.
36 /// The value of the <see cref="P:MemberBinding.BindingType"/> property of a <see cref="MemberMemberBinding"/> object is <see cref="MemberBinding"/>.
38 public sealed class MemberMemberBinding : MemberBinding {
39 ReadOnlyCollection<MemberBinding> _bindings;
40 internal MemberMemberBinding(MemberInfo member, ReadOnlyCollection<MemberBinding> bindings)
41 #pragma warning disable 618
42 : base(MemberBindingType.MemberBinding, member) {
43 #pragma warning restore 618
48 /// Gets the bindings that describe how to initialize the members of a member.
50 public ReadOnlyCollection<MemberBinding> Bindings {
51 get { return _bindings; }
55 /// Creates a new expression that is like this one, but using the
56 /// supplied children. If all of the children are the same, it will
57 /// return this expression.
59 /// <param name="bindings">The <see cref="Bindings" /> property of the result.</param>
60 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
61 public MemberMemberBinding Update(IEnumerable<MemberBinding> bindings) {
62 if (bindings == Bindings) {
65 return Expression.MemberBind(Member, bindings);
70 public partial class Expression {
72 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property.
74 /// <param name="member">The <see cref="MemberInfo"/> to set the <see cref="P:MemberBinding.Member"/> property equal to.</param>
75 /// <param name="bindings">An array of <see cref="MemberBinding"/> objects to use to populate the <see cref="P:MemberMemberBindings.Bindings"/> collection.</param>
76 /// <returns>A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/> and the <see cref="P:MemberBinding.Member"/> and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.</returns>
77 public static MemberMemberBinding MemberBind(MemberInfo member, params MemberBinding[] bindings) {
78 ContractUtils.RequiresNotNull(member, "member");
79 ContractUtils.RequiresNotNull(bindings, "bindings");
80 return MemberBind(member, (IEnumerable<MemberBinding>)bindings);
84 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property.
86 /// <param name="member">The <see cref="MemberInfo"/> to set the <see cref="P:MemberBinding.Member"/> property equal to.</param>
87 /// <param name="bindings">An <see cref="IEnumerable{T}"/> that contains <see cref="MemberBinding"/> objects to use to populate the <see cref="P:MemberMemberBindings.Bindings"/> collection.</param>
88 /// <returns>A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/> and the <see cref="P:MemberBinding.Member"/> and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.</returns>
89 public static MemberMemberBinding MemberBind(MemberInfo member, IEnumerable<MemberBinding> bindings) {
90 ContractUtils.RequiresNotNull(member, "member");
91 ContractUtils.RequiresNotNull(bindings, "bindings");
92 ReadOnlyCollection<MemberBinding> roBindings = bindings.ToReadOnly();
94 ValidateGettableFieldOrPropertyMember(member, out memberType);
95 ValidateMemberInitArgs(memberType, roBindings);
96 return new MemberMemberBinding(member, roBindings);
100 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a member that is accessed by using a property accessor method.
102 /// <param name="propertyAccessor">The <see cref="MemberInfo"/> that represents a property accessor method.</param>
103 /// <param name="bindings">An <see cref="IEnumerable{T}"/> that contains <see cref="MemberBinding"/> objects to use to populate the <see cref="P:MemberMemberBindings.Bindings"/> collection.</param>
105 /// A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/>,
106 /// the Member property set to the <see cref="PropertyInfo"/> that represents the property accessed in <paramref name="propertyAccessor"/>,
107 /// and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.
109 public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, params MemberBinding[] bindings) {
110 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
111 return MemberBind(GetProperty(propertyAccessor), bindings);
115 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a member that is accessed by using a property accessor method.
117 /// <param name="propertyAccessor">The <see cref="MemberInfo"/> that represents a property accessor method.</param>
118 /// <param name="bindings">An <see cref="IEnumerable{T}"/> that contains <see cref="MemberBinding"/> objects to use to populate the <see cref="P:MemberMemberBindings.Bindings"/> collection.</param>
120 /// A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/>,
121 /// the Member property set to the <see cref="PropertyInfo"/> that represents the property accessed in <paramref name="propertyAccessor"/>,
122 /// and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.
124 public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings) {
125 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
126 return MemberBind(GetProperty(propertyAccessor), bindings);
129 private static void ValidateGettableFieldOrPropertyMember(MemberInfo member, out Type memberType) {
130 FieldInfo fi = member as FieldInfo;
132 PropertyInfo pi = member as PropertyInfo;
134 throw Error.ArgumentMustBeFieldInfoOrPropertInfo();
137 throw Error.PropertyDoesNotHaveGetter(pi);
139 memberType = pi.PropertyType;
141 memberType = fi.FieldType;
145 private static void ValidateMemberInitArgs(Type type, ReadOnlyCollection<MemberBinding> bindings) {
146 for (int i = 0, n = bindings.Count; i < n; i++) {
147 MemberBinding b = bindings[i];
148 ContractUtils.RequiresNotNull(b, "bindings");
149 if (!b.Member.DeclaringType.IsAssignableFrom(type)) {
150 throw Error.NotAMemberOfType(b.Member.Name, type);