PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. #if !CLR2
  16. using System.Linq.Expressions;
  17. #else
  18. using Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Dynamic;
  23. using Microsoft.Scripting.Actions;
  24. using Microsoft.Scripting.Runtime;
  25. using Microsoft.Scripting.ComInterop;
  26. namespace SiteTest.Actions {
  27. class TestGetMemberBinder : GetMemberBinder {
  28. internal TestGetMemberBinder(string name)
  29. : base(name, false) {
  30. }
  31. internal TestGetMemberBinder(string name, bool ignoreCase)
  32. : base(name, ignoreCase) {
  33. }
  34. private int _fakeId = 0;
  35. private int[] _fakeData;
  36. internal TestGetMemberBinder(string name, bool ignoreCase, int fakeId)
  37. : base(name, ignoreCase) {
  38. _fakeId = fakeId;
  39. // why don't we grab some RAM.
  40. _fakeData = new int[100000];
  41. }
  42. public override int GetHashCode() {
  43. return base.GetHashCode() ^ _fakeId;
  44. }
  45. public override bool Equals(object obj) {
  46. TestGetMemberBinder other = obj as TestGetMemberBinder;
  47. return base.Equals(other) && _fakeId == other._fakeId;
  48. }
  49. public override DynamicMetaObject FallbackGetMember(DynamicMetaObject self, DynamicMetaObject onBindingError) {
  50. DynamicMetaObject com;
  51. if (ComBinder.TryBindGetMember(this, self, out com, true))
  52. return com;
  53. //The language implementation to get a special member
  54. if (String.Compare(Name, "__code__", IgnoreCase) == 0) {
  55. return new DynamicMetaObject(
  56. Expression.Constant("123"),
  57. self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
  58. );
  59. }
  60. //The language implementation to get a special member
  61. if (String.Compare(Name, "pLong", IgnoreCase) == 0) {
  62. return new DynamicMetaObject(
  63. Expression.Constant("777"),
  64. self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
  65. );
  66. }
  67. return onBindingError ?? new DynamicMetaObject(
  68. Expression.Throw(Expression.New(typeof(BindingException)), typeof(object)),
  69. self.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(self.Expression, self.LimitType))
  70. );
  71. }
  72. }
  73. public class BindingException : Exception { //@TODO - Pull this out and fill it in
  74. public BindingException()
  75. : base() {
  76. }
  77. public BindingException(string msg)
  78. : base(msg) {
  79. }
  80. }
  81. }