Fix potential order inconsistency of element and property lists.

This commit is contained in:
2023-03-03 11:45:16 +08:00
parent 215f72b3b5
commit da60dc0903
12 changed files with 134 additions and 27 deletions

View File

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