Merge pull request #1109 from adbre/iss358
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / ComFallbackMetaObject.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 !SILVERLIGHT
19
20 #if CODEPLEX_40
21 using System.Linq.Expressions;
22 using System.Dynamic;
23 using System.Dynamic.Utils;
24 #else
25 using Microsoft.Linq.Expressions;
26 using Microsoft.Scripting;
27 using Microsoft.Scripting.Utils;
28 #endif
29 using System.Diagnostics;
30
31 #if CODEPLEX_40
32 namespace System.Dynamic {
33 #else
34 namespace Microsoft.Scripting {
35 #endif
36     //
37     // ComFallbackMetaObject just delegates everything to the binder.
38     //
39     // Note that before performing FallBack on a ComObject we need to unwrap it so that
40     // binder would act upon the actual object (typically Rcw)
41     //
42     // Also: we don't need to implement these for any operations other than those
43     // supported by ComBinder
44     internal class ComFallbackMetaObject : DynamicMetaObject {
45         internal ComFallbackMetaObject(Expression expression, BindingRestrictions restrictions, object arg)
46             : base(expression, restrictions, arg) {
47         }
48
49         public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes) {
50             ContractUtils.RequiresNotNull(binder, "binder");
51             return binder.FallbackGetIndex(UnwrapSelf(), indexes);
52         }
53
54         public override DynamicMetaObject BindSetIndex(SetIndexBinder binder, DynamicMetaObject[] indexes, DynamicMetaObject value) {
55             ContractUtils.RequiresNotNull(binder, "binder");
56             return binder.FallbackSetIndex(UnwrapSelf(), indexes, value);
57         }
58
59         public override DynamicMetaObject BindGetMember(GetMemberBinder binder) {
60             ContractUtils.RequiresNotNull(binder, "binder");
61             return binder.FallbackGetMember(UnwrapSelf());
62         }
63
64         public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args) {
65             ContractUtils.RequiresNotNull(binder, "binder");
66             return binder.FallbackInvokeMember(UnwrapSelf(), args);
67         }
68
69         public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value) {
70             ContractUtils.RequiresNotNull(binder, "binder");
71             return binder.FallbackSetMember(UnwrapSelf(), value);
72         }
73
74         protected virtual ComUnwrappedMetaObject UnwrapSelf() {
75             return new ComUnwrappedMetaObject(
76                 ComObject.RcwFromComObject(Expression),
77                 Restrictions.Merge(ComBinderHelpers.GetTypeRestrictionForDynamicMetaObject(this)),
78                 ((ComObject)Value).RuntimeCallableWrapper
79             );
80         }
81     }
82
83     // This type exists as a signal type, so ComBinder knows not to try to bind
84     // again when we're trying to fall back
85     internal sealed class ComUnwrappedMetaObject : DynamicMetaObject {
86         internal ComUnwrappedMetaObject(Expression expression, BindingRestrictions restrictions, object value)
87             : base(expression, restrictions, value) {
88         }
89     }
90 }
91
92 #endif