PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Dynamic/ComInterop/TypeLibMetaObject.cs

#
C# | 86 lines | 59 code | 13 blank | 14 comment | 3 complexity | d78828c77075197e72c905625f8026b0 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 !SILVERLIGHT
  16. #if !CLR2
  17. using System.Linq.Expressions;
  18. #else
  19. using Microsoft.Scripting.Ast;
  20. #endif
  21. using System.Collections.Generic;
  22. using System.Dynamic;
  23. using AstUtils = Microsoft.Scripting.Ast.Utils;
  24. namespace Microsoft.Scripting.ComInterop {
  25. internal class TypeLibMetaObject : DynamicMetaObject {
  26. private readonly ComTypeLibDesc _lib;
  27. internal TypeLibMetaObject(Expression expression, ComTypeLibDesc lib)
  28. : base(expression, BindingRestrictions.Empty, lib) {
  29. _lib = lib;
  30. }
  31. private DynamicMetaObject TryBindGetMember(string name) {
  32. if (_lib.HasMember(name)) {
  33. BindingRestrictions restrictions =
  34. BindingRestrictions.GetTypeRestriction(
  35. Expression, typeof(ComTypeLibDesc)
  36. ).Merge(
  37. BindingRestrictions.GetExpressionRestriction(
  38. Expression.Equal(
  39. Expression.Property(
  40. AstUtils.Convert(
  41. Expression, typeof(ComTypeLibDesc)
  42. ),
  43. typeof(ComTypeLibDesc).GetProperty("Guid")
  44. ),
  45. AstUtils.Constant(_lib.Guid)
  46. )
  47. )
  48. );
  49. return new DynamicMetaObject(
  50. AstUtils.Constant(
  51. ((ComTypeLibDesc)Value).GetTypeLibObjectDesc(name)
  52. ),
  53. restrictions
  54. );
  55. }
  56. return null;
  57. }
  58. public override DynamicMetaObject BindGetMember(GetMemberBinder binder) {
  59. return TryBindGetMember(binder.Name) ?? base.BindGetMember(binder);
  60. }
  61. public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args) {
  62. var result = TryBindGetMember(binder.Name);
  63. if (result != null) {
  64. return binder.FallbackInvoke(result, args, null);
  65. }
  66. return base.BindInvokeMember(binder, args);
  67. }
  68. public override IEnumerable<string> GetDynamicMemberNames() {
  69. return _lib.GetMemberNames();
  70. }
  71. }
  72. }
  73. #endif