我的第一个建议是使用自定义比较器,但他没有解决问题。因此,我更详细地调查了 SortedList 的实现,并用以下建议替换了我原来的帖子:
覆盖 Add 方法并使用反射调用私有 Insert 应该可以解决问题
private MySortedList()
{
}
public override void Add(object key, object value)
{
if (key == null || value == null)
{
//throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
throw new ArgumentNullException(); // build your own exception, Environment.GetResourceString is not accessible here
}
var valuesArray = new object[Values.Count];
Values.CopyTo(valuesArray , 0);
int index = Array.BinarySearch(valuesArray, 0, valuesArray.Length, value, _comparer);
if (index >= 0)
{
//throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicate__", new object[] { this.GetKey(index), key }));
throw new ArgumentNullException(); // build your own exception, Environment.GetResourceString is not accessible here
}
MethodInfo m = typeof(SortedList).GetMethod("Insert", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(this, new object[] {~index, key, value});
}