Created
November 2, 2026 23:12
-
-
Save StartAutomating/8b9b61fe70eddb04c6e77ca05469eee4 to your computer and use it in GitHub Desktop.
GistWriteMeADictionaryWrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Dictionary Wrapper | |
| .DESCRIPTION | |
| Writes a wrapper class for a dictionary. | |
| #> | |
| param( | |
| [string] | |
| $TypeName = 'Foo', | |
| [string] | |
| $Namespace | |
| ) | |
| $TypeDefinition = @" | |
| using System; | |
| using System.Collections; | |
| using System.Collections.Specialized; | |
| public class $typeName : System.Collections.IDictionary { | |
| private OrderedDictionary inner = new OrderedDictionary(StringComparer.InvariantCultureIgnoreCase); | |
| // Construct the SimpleDictionary with the desired number of items. | |
| // The number of items cannot change for the life time of this SimpleDictionary. | |
| public $typeName() | |
| { | |
| } | |
| #region IDictionary Members | |
| public bool IsReadOnly { get { return inner.IsReadOnly; } } | |
| public bool Contains(object key) | |
| { | |
| return this.inner.Contains(key); | |
| } | |
| public bool IsFixedSize { get { return false; } } | |
| public void Remove(object key) | |
| { | |
| this.inner.Remove(key); | |
| } | |
| public void Clear() { inner.Clear(); } | |
| public void Add(object key, object value) | |
| { | |
| this.inner.Add(key, value); | |
| } | |
| public ICollection Keys { get { return this.inner.Keys; } } | |
| public ICollection Values { get { return this.inner.Values; } } | |
| public object this[object key] { | |
| get { return this.inner[key]; } | |
| set { this.inner[key] = value; } | |
| } | |
| public IDictionaryEnumerator GetEnumerator() { return inner.GetEnumerator(); } | |
| #endregion | |
| #region ICollection Members | |
| public bool IsSynchronized { | |
| get { return false; } | |
| } | |
| public object SyncRoot { | |
| get { return null; } | |
| } | |
| public int Count { | |
| get { return this.inner.Count; } | |
| } | |
| public void CopyTo(Array array, int index) { | |
| this.inner.CopyTo(array, index); | |
| } | |
| #endregion | |
| #region IEnumerable Members | |
| IEnumerator IEnumerable.GetEnumerator() | |
| { | |
| // Construct and return an enumerator. | |
| return ((IDictionary)this).GetEnumerator(); | |
| } | |
| #endregion | |
| } | |
| "@ | |
| if ($namespace) { | |
| "namespace $namespace {" | |
| foreach ($line in $TypeDefinition -split '(?>\r\n|\n)') { | |
| (' ' * 4) + $line | |
| } | |
| "}" | |
| } else { | |
| $TypeDefinition | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment