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