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 * ***************************************************************************/
15 using System; using Microsoft;
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
21 using System.Dynamic.Utils;
23 using Microsoft.Scripting.Utils;
25 using System.Reflection;
28 namespace System.Linq.Expressions {
30 namespace Microsoft.Linq.Expressions {
33 /// Represents initializing members of a member of a newly created object.
36 /// Use the <see cref="M:MemberBind"/> factory methods to create a <see cref="MemberMemberBinding"/>.
37 /// The value of the <see cref="P:MemberBinding.BindingType"/> property of a <see cref="MemberMemberBinding"/> object is <see cref="MemberBinding"/>.
39 public sealed class MemberMemberBinding : MemberBinding {
40 ReadOnlyCollection<MemberBinding> _bindings;
41 internal MemberMemberBinding(MemberInfo member, ReadOnlyCollection<MemberBinding> bindings)
42 #pragma warning disable 618
43 : base(MemberBindingType.MemberBinding, member) {
44 #pragma warning restore 618
49 /// Gets the bindings that describe how to initialize the members of a member.
51 public ReadOnlyCollection<MemberBinding> Bindings {
52 get { return _bindings; }
57 public partial class Expression {
59 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property.
61 /// <param name="member">The <see cref="MemberInfo"/> to set the <see cref="P:MemberBinding.Member"/> property equal to.</param>
62 /// <param name="bindings">An array of <see cref="MemberBinding"/> objects to use to populate the <see cref="P:MemberMemberBindings.Bindings"/> collection.</param>
63 /// <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>
64 public static MemberMemberBinding MemberBind(MemberInfo member, params MemberBinding[] bindings) {
65 ContractUtils.RequiresNotNull(member, "member");
66 ContractUtils.RequiresNotNull(bindings, "bindings");
67 return MemberBind(member, (IEnumerable<MemberBinding>)bindings);
71 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property.
73 /// <param name="member">The <see cref="MemberInfo"/> to set the <see cref="P:MemberBinding.Member"/> property equal to.</param>
74 /// <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>
75 /// <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>
76 public static MemberMemberBinding MemberBind(MemberInfo member, IEnumerable<MemberBinding> bindings) {
77 ContractUtils.RequiresNotNull(member, "member");
78 ContractUtils.RequiresNotNull(bindings, "bindings");
79 ReadOnlyCollection<MemberBinding> roBindings = bindings.ToReadOnly();
81 ValidateGettableFieldOrPropertyMember(member, out memberType);
82 ValidateMemberInitArgs(memberType, roBindings);
83 return new MemberMemberBinding(member, roBindings);
87 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a member that is accessed by using a property accessor method.
89 /// <param name="propertyAccessor">The <see cref="MemberInfo"/> that represents a property accessor method.</param>
90 /// <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>
92 /// A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/>,
93 /// the Member property set to the <see cref="PropertyInfo"/> that represents the property accessed in <paramref name="propertyAccessor"/>,
94 /// and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.
96 public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, params MemberBinding[] bindings) {
97 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
98 return MemberBind(GetProperty(propertyAccessor), bindings);
102 /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a member that is accessed by using a property accessor method.
104 /// <param name="propertyAccessor">The <see cref="MemberInfo"/> that represents a property accessor method.</param>
105 /// <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>
107 /// A <see cref="MemberMemberBinding"/> that has the <see cref="P:MemberBinding.BindingType"/> property equal to <see cref="MemberBinding"/>,
108 /// the Member property set to the <see cref="PropertyInfo"/> that represents the property accessed in <paramref name="propertyAccessor"/>,
109 /// and <see cref="P:MemberMemberBindings.Bindings"/> properties set to the specified values.
111 public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings) {
112 ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
113 return MemberBind(GetProperty(propertyAccessor), bindings);
116 private static void ValidateGettableFieldOrPropertyMember(MemberInfo member, out Type memberType) {
117 FieldInfo fi = member as FieldInfo;
119 PropertyInfo pi = member as PropertyInfo;
121 throw Error.ArgumentMustBeFieldInfoOrPropertInfo();
124 throw Error.PropertyDoesNotHaveGetter(pi);
126 memberType = pi.PropertyType;
128 memberType = fi.FieldType;
132 private static void ValidateMemberInitArgs(Type type, ReadOnlyCollection<MemberBinding> bindings) {
133 for (int i = 0, n = bindings.Count; i < n; i++) {
134 MemberBinding b = bindings[i];
135 ContractUtils.RequiresNotNull(b, "bindings");
136 if (!b.Member.DeclaringType.IsAssignableFrom(type)) {
137 throw Error.NotAMemberOfType(b.Member.Name, type);