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