2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / MemberMemberBinding.cs
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 using System; using Microsoft;
16
17
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 #if CODEPLEX_40
21 using System.Dynamic.Utils;
22 #else
23 using Microsoft.Scripting.Utils;
24 #endif
25 using System.Reflection;
26
27 #if CODEPLEX_40
28 namespace System.Linq.Expressions {
29 #else
30 namespace Microsoft.Linq.Expressions {
31 #endif
32     /// <summary>
33     /// Represents initializing members of a member of a newly created object.
34     /// </summary>
35     /// <remarks>
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"/>. 
38     /// </remarks>
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
45             _bindings = bindings;
46         }
47
48         /// <summary>
49         /// Gets the bindings that describe how to initialize the members of a member. 
50         /// </summary>
51         public ReadOnlyCollection<MemberBinding> Bindings {
52             get { return _bindings; }
53         }
54     }
55     
56
57     public partial class Expression {
58         /// <summary>
59         /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property. 
60         /// </summary>
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);
68         }
69         
70         /// <summary>
71         /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property. 
72         /// </summary>
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();
80             Type memberType;
81             ValidateGettableFieldOrPropertyMember(member, out memberType);
82             ValidateMemberInitArgs(memberType, roBindings);
83             return new MemberMemberBinding(member, roBindings);
84         }
85
86         /// <summary>
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.  
88         /// </summary>
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>
91         /// <returns>
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.
95         /// </returns>
96         public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, params MemberBinding[] bindings) {
97             ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
98             return MemberBind(GetProperty(propertyAccessor), bindings);
99         }
100
101         /// <summary>
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.  
103         /// </summary>
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>
106         /// <returns>
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.
110         /// </returns>
111         public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings) {
112             ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
113             return MemberBind(GetProperty(propertyAccessor), bindings);
114         }
115
116         private static void ValidateGettableFieldOrPropertyMember(MemberInfo member, out Type memberType) {
117             FieldInfo fi = member as FieldInfo;
118             if (fi == null) {
119                 PropertyInfo pi = member as PropertyInfo;
120                 if (pi == null) {
121                     throw Error.ArgumentMustBeFieldInfoOrPropertInfo();
122                 }
123                 if (!pi.CanRead) {
124                     throw Error.PropertyDoesNotHaveGetter(pi);
125                 }
126                 memberType = pi.PropertyType;
127             } else {
128                 memberType = fi.FieldType;
129             }
130         }
131         
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);
138                 }
139             }
140         }
141     }
142 }