2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / SetMemberBinder.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 #if CODEPLEX_40
19 using System.Dynamic.Utils;
20 #else
21 using Microsoft.Scripting.Utils;
22 #endif
23
24 #if CODEPLEX_40
25 namespace System.Dynamic {
26 #else
27 namespace Microsoft.Scripting {
28 #endif
29     /// <summary>
30     /// Represents the dynamic set member operation at the call site, providing the binding semantic and the details about the operation.
31     /// </summary>
32     public abstract class SetMemberBinder : DynamicMetaObjectBinder {
33         private readonly string _name;
34         private readonly bool _ignoreCase;
35
36         /// <summary>
37         /// Initializes a new instance of the <see cref="SetMemberBinder" />.
38         /// </summary>
39         /// <param name="name">The name of the member to get.</param>
40         /// <param name="ignoreCase">true if the name should be matched ignoring case; false otherwise.</param>
41         protected SetMemberBinder(string name, bool ignoreCase) {
42             ContractUtils.RequiresNotNull(name, "name");
43
44             _name = name;
45             _ignoreCase = ignoreCase;
46         }
47
48         /// <summary>
49         /// The result type of the operation.
50         /// </summary>
51         public override sealed Type ReturnType {
52             get { return typeof(object); }
53         }
54
55         /// <summary>
56         /// Gets the name of the member to get.
57         /// </summary>
58         public string Name {
59             get {
60                 return _name;
61             }
62         }
63
64         /// <summary>
65         /// Gets the value indicating if the string comparison should ignore the case of the member name.
66         /// </summary>
67         public bool IgnoreCase {
68             get {
69                 return _ignoreCase;
70             }
71         }
72
73         /// <summary>
74         /// Performs the binding of the dynamic set member operation.
75         /// </summary>
76         /// <param name="target">The target of the dynamic set member operation.</param>
77         /// <param name="args">An array of arguments of the dynamic set member operation.</param>
78         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
79         public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
80             ContractUtils.RequiresNotNull(target, "target");
81             ContractUtils.RequiresNotNull(args, "args");
82             ContractUtils.Requires(args.Length == 1, "args");
83
84             var arg0 = args[0];
85             ContractUtils.RequiresNotNull(arg0, "args");
86
87             return target.BindSetMember(this, arg0);
88         }
89
90         // this is a standard DynamicMetaObjectBinder
91         internal override sealed bool IsStandardBinder {
92             get {
93                 return true;
94             }
95         }
96
97         /// <summary>
98         /// Performs the binding of the dynamic set member operation if the target dynamic object cannot bind.
99         /// </summary>
100         /// <param name="target">The target of the dynamic set member operation.</param>
101         /// <param name="value">The value to set to the member.</param>
102         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
103         public DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value) {
104             return FallbackSetMember(target, value, null);
105         }
106
107         /// <summary>
108         /// Performs the binding of the dynamic set member operation if the target dynamic object cannot bind.
109         /// </summary>
110         /// <param name="target">The target of the dynamic set member operation.</param>
111         /// <param name="value">The value to set to the member.</param>
112         /// <param name="errorSuggestion">The binding result to use if binding fails, or null.</param>
113         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
114         public abstract DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion);
115     }
116 }