Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ComTypeEnumDesc.cs93 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.Dynamic;6using System.Globalization;7using System.Linq.Expressions;8using System.Runtime.InteropServices;9using ComTypes = System.Runtime.InteropServices.ComTypes;10 11namespace System.Management.Automation.ComInterop12{13    internal sealed class ComTypeEnumDesc : ComTypeDesc, IDynamicMetaObjectProvider14    {15        private readonly string[] _memberNames;16        private readonly object[] _memberValues;17 18        public override string ToString() => $"<enum '{TypeName}'>";19 20        internal ComTypeEnumDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, typeLibDesc)21        {22            ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo);23            string[] memberNames = new string[typeAttr.cVars];24            object[] memberValues = new object[typeAttr.cVars];25 26            IntPtr p;27 28            // For each enum member get name and value.29            for (int i = 0; i < typeAttr.cVars; i++)30            {31                typeInfo.GetVarDesc(i, out p);32 33                // Get the enum member value (as object).34                ComTypes.VARDESC varDesc;35 36                try37                {38                    varDesc = (ComTypes.VARDESC)Marshal.PtrToStructure(p, typeof(ComTypes.VARDESC));39 40                    if (varDesc.varkind == ComTypes.VARKIND.VAR_CONST)41                    {42                        memberValues[i] = Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue);43                    }44                }45                finally46                {47                    typeInfo.ReleaseVarDesc(p);48                }49 50                // Get the enum member name51                memberNames[i] = ComRuntimeHelpers.GetNameOfMethod(typeInfo, varDesc.memid);52            }53 54            _memberNames = memberNames;55            _memberValues = memberValues;56        }57 58        DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)59        {60            return new TypeEnumMetaObject(this, parameter);61        }62 63        public object GetValue(string enumValueName)64        {65            for (int i = 0; i < _memberNames.Length; i++)66            {67                if (_memberNames[i] == enumValueName)68                {69                    return _memberValues[i];70                }71            }72 73            throw new MissingMemberException(enumValueName);74        }75 76        internal bool HasMember(string name)77        {78            for (int i = 0; i < _memberNames.Length; i++)79            {80                if (_memberNames[i] == name)81                    return true;82            }83 84            return false;85        }86 87        public string[] GetMemberNames()88        {89            return (string[])_memberNames.Clone();90        }91    }92}93