Supplement generic PairCollection.

This commit is contained in:
2023-03-03 13:26:11 +08:00
parent 2e54b38d2b
commit 7736eba14d

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Cryville.Common.Collections.Generic {
public struct PairCollection : IDisposable {
@@ -17,4 +18,19 @@ namespace Cryville.Common.Collections.Generic {
else _dictionary.Add(key, value);
}
}
public struct PairCollection<TKey, TValue> : IDisposable {
public void Dispose() { }
IPairList<TKey, TValue> _pairList;
IDictionary<TKey, TValue> _dictionary;
public PairCollection(object collection) : this() {
var type = collection.GetType();
if (typeof(IPairList<TKey, TValue>).IsAssignableFrom(type)) _pairList = (IPairList<TKey, TValue>)collection;
else if (typeof(IDictionary<TKey, TValue>).IsAssignableFrom(type)) _dictionary = (IDictionary<TKey, TValue>)collection;
else throw new ArgumentException("Parameter is not a pair collection");
}
public void Add(TKey key, TValue value) {
if (_pairList != null) _pairList.Add(key, value);
else _dictionary.Add(key, value);
}
}
}