PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/SiteTest/Actions/TestGetMemberBinder.cs

#
C# | 98 lines | 64 code | 17 blank | 17 comment | 7 complexity | 0e822c680553e61c284948c18c66cc48 MD5 | raw file
 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#if !CLR2
17using System.Linq.Expressions;
18#else
19using Microsoft.Scripting.Ast;
20#endif
21
22using System;
23using System.Collections.Generic;
24using System.Dynamic;
25using Microsoft.Scripting.Actions;
26using Microsoft.Scripting.Runtime;
27using Microsoft.Scripting.ComInterop;
28
29namespace SiteTest.Actions {
30    class TestGetMemberBinder : GetMemberBinder {
31        internal TestGetMemberBinder(string name)
32            : base(name, false) {
33        }
34
35        internal TestGetMemberBinder(string name, bool ignoreCase)
36            : base(name, ignoreCase) {
37        }
38
39        private int _fakeId = 0;
40        private int[] _fakeData;
41
42        internal TestGetMemberBinder(string name, bool ignoreCase, int fakeId)
43            : base(name, ignoreCase) {
44            _fakeId = fakeId;
45            // why don't we grab some RAM.
46            _fakeData = new int[100000];
47        }
48
49        public override int GetHashCode() {
50            return base.GetHashCode() ^ _fakeId;
51        }
52
53        public override bool Equals(object obj) {
54            TestGetMemberBinder other = obj as TestGetMemberBinder;
55            return base.Equals(other) && _fakeId == other._fakeId;
56        }
57
58
59
60        public override DynamicMetaObject FallbackGetMember(DynamicMetaObject self, DynamicMetaObject onBindingError) {
61            DynamicMetaObject com;
62
63            if (ComBinder.TryBindGetMember(this, self, out com, true))
64                return com;
65
66            //The language implementation to get a special member
67            if (String.Compare(Name, "__code__", IgnoreCase) == 0) {
68                return new DynamicMetaObject(
69                    Expression.Constant("123"),
70                    self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
71                );
72            }
73
74            //The language implementation to get a special member
75            if (String.Compare(Name, "pLong", IgnoreCase) == 0) {
76                return new DynamicMetaObject(
77                    Expression.Constant("777"),
78                    self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
79                );
80            }
81
82            return onBindingError ?? new DynamicMetaObject(
83                Expression.Throw(Expression.New(typeof(BindingException)), typeof(object)),
84                self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
85            );
86        }
87    }
88
89    public class BindingException : Exception { //@TODO - Pull this out and fill it in
90        public BindingException()
91            : base() {
92        }
93
94        public BindingException(string msg)
95            : base(msg) {
96        }
97    }
98}