Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
TypeLibMetaObject.cs75 linesDownload Raw Back to ComInterop
1// Licensed to the .NET Foundation under one or more agreements.2// The .NET Foundation licenses this file to you under the MIT license.3 4using System.Collections.Generic;5using System.Dynamic;6using System.Linq.Expressions;7using AstUtils = System.Management.Automation.Interpreter.Utils;8 9namespace System.Management.Automation.ComInterop10{11    internal class TypeLibMetaObject : DynamicMetaObject12    {13        private readonly ComTypeLibDesc _lib;14 15        internal TypeLibMetaObject(Expression expression, ComTypeLibDesc lib)16            : base(expression, BindingRestrictions.Empty, lib)17        {18            _lib = lib;19        }20 21        private DynamicMetaObject TryBindGetMember(string name)22        {23            if (_lib.HasMember(name))24            {25                BindingRestrictions restrictions =26                    BindingRestrictions.GetTypeRestriction(27                        Expression, typeof(ComTypeLibDesc)28                    ).Merge(29                        BindingRestrictions.GetExpressionRestriction(30                            Expression.Equal(31                                Expression.Property(32                                    AstUtils.Convert(33                                        Expression, typeof(ComTypeLibDesc)34                                    ),35                                    typeof(ComTypeLibDesc).GetProperty(nameof(ComTypeLibDesc.Guid))36                                ),37                                Expression.Constant(_lib.Guid)38                            )39                        )40                    );41 42                return new DynamicMetaObject(43                    Expression.Constant(44                        ((ComTypeLibDesc)Value).GetTypeLibObjectDesc(name)45                    ),46                    restrictions47                );48            }49 50            return null;51        }52 53        public override DynamicMetaObject BindGetMember(GetMemberBinder binder)54        {55            return TryBindGetMember(binder.Name) ?? base.BindGetMember(binder);56        }57 58        public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)59        {60            DynamicMetaObject result = TryBindGetMember(binder.Name);61            if (result != null)62            {63                return binder.FallbackInvoke(result, args, null);64            }65 66            return base.BindInvokeMember(binder, args);67        }68 69        public override IEnumerable<string> GetDynamicMemberNames()70        {71            return _lib.GetMemberNames();72        }73    }74}75