HashtableExtensions.cs (1927B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="HashtableExtensions.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.Processor.Extensions 8 { 9 using System.Collections; 10 using Microsoft.Management.Configuration.Processor.Exceptions; 11 using Microsoft.Management.Configuration.Processor.Helpers; 12 using Windows.Foundation.Collections; 13 14 /// <summary> 15 /// Extensions for Hashtable. 16 /// </summary> 17 internal static class HashtableExtensions 18 { 19 /// <summary> 20 /// Convert a hashtable to a value set. 21 /// </summary> 22 /// <param name="hashtable">hashtable.</param> 23 /// <returns>Value set.</returns> 24 public static ValueSet ToValueSet(this Hashtable hashtable) 25 { 26 var valueSet = new ValueSet(); 27 28 foreach (DictionaryEntry entry in hashtable) 29 { 30 if (entry.Key is string key) 31 { 32 if (entry.Value is null) 33 { 34 valueSet.Add(key, null); 35 } 36 else 37 { 38 var value = TypeHelpers.GetCompatibleValueSetValueOfProperty(entry.Value.GetType(), entry.Value); 39 if (value != null) 40 { 41 valueSet.Add(key, value); 42 } 43 } 44 } 45 else 46 { 47 throw new UnitPropertyUnsupportedException(entry.Key.GetType()); 48 } 49 } 50 51 return valueSet; 52 } 53 } 54 }