PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting.Core/Com/ComMetaObject.cs

#
C# | 108 lines | 74 code | 19 blank | 15 comment | 2 complexity | 1009a42f1a9ff591b8db99c31de7472e 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 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. #if !SILVERLIGHT
  17. using Microsoft.Linq.Expressions;
  18. using Microsoft.Scripting.Actions;
  19. using Microsoft.Scripting.Utils;
  20. namespace Microsoft.Scripting.Com {
  21. internal class ComMetaObject : MetaObject {
  22. internal ComMetaObject(Expression expression, Restrictions restrictions, object arg)
  23. : base(expression, restrictions, arg) {
  24. }
  25. #region MetaObject
  26. public override MetaObject Call(CallAction action, MetaObject[] args) {
  27. ContractUtils.RequiresNotNull(action, "action");
  28. return action.Defer(args.AddFirst(WrapSelf()));
  29. }
  30. public override MetaObject Convert(ConvertAction action) {
  31. ContractUtils.RequiresNotNull(action, "action");
  32. return action.Defer(WrapSelf());
  33. }
  34. public override MetaObject Create(CreateAction action, MetaObject[] args) {
  35. ContractUtils.RequiresNotNull(action, "action");
  36. return action.Defer(args.AddFirst(WrapSelf()));
  37. }
  38. public override MetaObject DeleteMember(DeleteMemberAction action) {
  39. ContractUtils.RequiresNotNull(action, "action");
  40. return action.Defer(WrapSelf());
  41. }
  42. public override MetaObject GetMember(GetMemberAction action) {
  43. ContractUtils.RequiresNotNull(action, "action");
  44. return action.Defer(WrapSelf());
  45. }
  46. public override MetaObject Invoke(InvokeAction action, MetaObject[] args) {
  47. ContractUtils.RequiresNotNull(action, "action");
  48. return action.Defer(args.AddFirst(WrapSelf()));
  49. }
  50. public override MetaObject Operation(OperationAction action, MetaObject[] args) {
  51. ContractUtils.RequiresNotNull(action, "action");
  52. return action.Defer(args.AddFirst(WrapSelf()));
  53. }
  54. public override MetaObject SetMember(SetMemberAction action, MetaObject value) {
  55. ContractUtils.RequiresNotNull(action, "action");
  56. return action.Defer(WrapSelf(), value);
  57. }
  58. #endregion
  59. private MetaObject WrapSelf() {
  60. return new MetaObject(
  61. Expression.Call(
  62. typeof(ComObject).GetMethod("ObjectToComObject"),
  63. Expression.ConvertHelper(Expression, typeof(object))
  64. ),
  65. Restrictions.ExpressionRestriction(
  66. Expression.AndAlso(
  67. Expression.NotEqual(
  68. Expression.ConvertHelper(Expression, typeof(object)),
  69. Expression.Null()
  70. ),
  71. Expression.Call(
  72. typeof(System.Runtime.InteropServices.Marshal).GetMethod("IsComObject"),
  73. Expression.ConvertHelper(Expression, typeof(object))
  74. )
  75. )
  76. )
  77. );
  78. }
  79. internal static MetaObject GetComMetaObject(Expression expression, object arg) {
  80. return new ComMetaObject(expression, Restrictions.Empty, arg);
  81. }
  82. private static readonly Type ComObjectType = typeof(object).Assembly.GetType("System.__ComObject");
  83. internal static bool IsComObject(object obj) {
  84. // we can't use System.Runtime.InteropServices.Marshal.IsComObject(obj) since it doesn't work in partial trust
  85. return obj != null && ComObjectType.IsAssignableFrom(obj.GetType());
  86. }
  87. }
  88. }
  89. #endif