BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 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.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 using System;
17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Dynamic.Utils;
20 using System.Reflection;
21
22 #if SILVERLIGHT
23 using System.Core;
24 #endif
25
26 #if CLR2
27 namespace Microsoft.Scripting.Ast {
28 #else
29 namespace System.Linq.Expressions {
30 #endif
31     /// <summary>
32     /// Represents initializing members of a member of a newly created object.
33     /// </summary>
34     /// <remarks>
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"/>. 
37     /// </remarks>
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
44             _bindings = bindings;
45         }
46
47         /// <summary>
48         /// Gets the bindings that describe how to initialize the members of a member. 
49         /// </summary>
50         public ReadOnlyCollection<MemberBinding> Bindings {
51             get { return _bindings; }
52         }
53
54         /// <summary>
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.
58         /// </summary>
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) {
63                 return this;
64             }
65             return Expression.MemberBind(Member, bindings);
66         }
67     }
68     
69
70     public partial class Expression {
71         /// <summary>
72         /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property. 
73         /// </summary>
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);
81         }
82         
83         /// <summary>
84         /// Creates a <see cref="MemberMemberBinding"/> that represents the recursive initialization of members of a field or property. 
85         /// </summary>
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();
93             Type memberType;
94             ValidateGettableFieldOrPropertyMember(member, out memberType);
95             ValidateMemberInitArgs(memberType, roBindings);
96             return new MemberMemberBinding(member, roBindings);
97         }
98
99         /// <summary>
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.  
101         /// </summary>
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>
104         /// <returns>
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.
108         /// </returns>
109         public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, params MemberBinding[] bindings) {
110             ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
111             return MemberBind(GetProperty(propertyAccessor), bindings);
112         }
113
114         /// <summary>
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.  
116         /// </summary>
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>
119         /// <returns>
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.
123         /// </returns>
124         public static MemberMemberBinding MemberBind(MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings) {
125             ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
126             return MemberBind(GetProperty(propertyAccessor), bindings);
127         }
128
129         private static void ValidateGettableFieldOrPropertyMember(MemberInfo member, out Type memberType) {
130             FieldInfo fi = member as FieldInfo;
131             if (fi == null) {
132                 PropertyInfo pi = member as PropertyInfo;
133                 if (pi == null) {
134                     throw Error.ArgumentMustBeFieldInfoOrPropertInfo();
135                 }
136                 if (!pi.CanRead) {
137                     throw Error.PropertyDoesNotHaveGetter(pi);
138                 }
139                 memberType = pi.PropertyType;
140             } else {
141                 memberType = fi.FieldType;
142             }
143         }
144         
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);
151                 }
152             }
153         }
154     }
155 }