2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / SetIndexBinder.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
30     /// <summary>
31     /// Represents the dynamic set index operation at the call site, providing the binding semantic and the details about the operation.
32     /// </summary>
33     public abstract class SetIndexBinder : DynamicMetaObjectBinder {
34         private readonly CallInfo _callInfo;
35
36         /// <summary>
37         /// Initializes a new instance of the <see cref="SetIndexBinder" />.
38         /// </summary>
39         /// <param name="callInfo">The signature of the arguments at the call site.</param>
40         protected SetIndexBinder(CallInfo callInfo) {
41             ContractUtils.RequiresNotNull(callInfo, "callInfo");
42             _callInfo = callInfo;
43         }
44
45         /// <summary>
46         /// The result type of the operation.
47         /// </summary>
48         public override sealed Type ReturnType {
49             get { return typeof(object); }
50         }
51
52         /// <summary>
53         /// Gets the signature of the arguments at the call site.
54         /// </summary>
55         public CallInfo CallInfo {
56             get { return _callInfo; }
57         }
58
59         /// <summary>
60         /// Performs the binding of the dynamic set index operation.
61         /// </summary>
62         /// <param name="target">The target of the dynamic set index operation.</param>
63         /// <param name="args">An array of arguments of the dynamic set index operation.</param>
64         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
65         public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
66             ContractUtils.RequiresNotNull(target, "target");
67             ContractUtils.RequiresNotNull(args, "args");
68             ContractUtils.Requires(args.Length >= 2, "args");
69
70             DynamicMetaObject value = args[args.Length - 1];
71             DynamicMetaObject[] indexes = args.RemoveLast();
72
73             ContractUtils.RequiresNotNull(value, "args");
74             ContractUtils.RequiresNotNullItems(indexes, "args");
75
76             return target.BindSetIndex(this, indexes, value);
77         }
78
79         // this is a standard DynamicMetaObjectBinder
80         internal override sealed bool IsStandardBinder {
81             get {
82                 return true;
83             }
84         }
85
86         /// <summary>
87         /// Performs the binding of the dynamic set index operation if the target dynamic object cannot bind.
88         /// </summary>
89         /// <param name="target">The target of the dynamic set index operation.</param>
90         /// <param name="indexes">The arguments of the dynamic set index operation.</param>
91         /// <param name="value">The value to set to the collection.</param>
92         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
93         public DynamicMetaObject FallbackSetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value) {
94             return FallbackSetIndex(target, indexes, value, null);
95         }
96
97         /// <summary>
98         /// When overridden in the derived class, performs the binding of the dynamic set index operation if the target dynamic object cannot bind.
99         /// </summary>
100         /// <param name="target">The target of the dynamic set index operation.</param>
101         /// <param name="indexes">The arguments of the dynamic set index operation.</param>
102         /// <param name="value">The value to set to the collection.</param>
103         /// <param name="errorSuggestion">The binding result to use if binding fails, or null.</param>
104         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
105         public abstract DynamicMetaObject FallbackSetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion);
106     }
107 }