Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
ComTypeClassDesc.cs74 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;5using System.Collections.Generic;6using System.Dynamic;7using System.Linq.Expressions;8using ComTypes = System.Runtime.InteropServices.ComTypes;9 10namespace System.Management.Automation.ComInterop11{12    internal class ComTypeClassDesc : ComTypeDesc, IDynamicMetaObjectProvider13    {14        private LinkedList<string> _itfs; // implemented interfaces15        private LinkedList<string> _sourceItfs; // source interfaces supported by this coclass16        private Type _typeObj;17 18        public object CreateInstance()19        {20            _typeObj ??= Type.GetTypeFromCLSID(Guid);21            return Activator.CreateInstance(Type.GetTypeFromCLSID(Guid));22        }23 24        internal ComTypeClassDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, typeLibDesc)25        {26            ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo);27            Guid = typeAttr.guid;28 29            for (int i = 0; i < typeAttr.cImplTypes; i++)30            {31                typeInfo.GetRefTypeOfImplType(i, out int hRefType);32                typeInfo.GetRefTypeInfo(hRefType, out ComTypes.ITypeInfo currentTypeInfo);33                typeInfo.GetImplTypeFlags(i, out ComTypes.IMPLTYPEFLAGS implTypeFlags);34 35                bool isSourceItf = (implTypeFlags & ComTypes.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0;36                AddInterface(currentTypeInfo, isSourceItf);37            }38        }39 40        private void AddInterface(ComTypes.ITypeInfo itfTypeInfo, bool isSourceItf)41        {42            string itfName = ComRuntimeHelpers.GetNameOfType(itfTypeInfo);43 44            if (isSourceItf)45            {46                _sourceItfs ??= new LinkedList<string>();47                _sourceItfs.AddLast(itfName);48            }49            else50            {51                _itfs ??= new LinkedList<string>();52                _itfs.AddLast(itfName);53            }54        }55 56        internal bool Implements(string itfName, bool isSourceItf)57        {58            if (isSourceItf)59                return _sourceItfs.Contains(itfName);60 61            return _itfs.Contains(itfName);62        }63 64        #region IDynamicMetaObjectProvider Members65 66        public DynamicMetaObject GetMetaObject(Expression parameter)67        {68            return new ComClassMetaObject(parameter, this);69        }70 71        #endregion72    }73}74