첫 번째 제안은 사용자 지정 비교자를 사용하는 것이지만 문제를 해결하지 못했습니다. 그래서 저는 더 자세한 SortedList implementaion을 조사하고 다음과 같은 제안에 내 원래의 게시물을 교체 :
는 Add 메서드를 재정의하고
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});
}
당신이 무엇에 어떤 요구 사항이 있습니까 트릭을해야 반사를 사용하여 개인 삽입을 호출 열쇠가 있어야합니까? 그렇지 않다면 텍스트를 키와 값으로 추가하지 않는 것이 어떻습니까? 'sortedList.Add (텍스트, 텍스트) ' – Douglas
예 첫 번째 매개 변수는 key 여야하고 두 번째 매개 변수는 value 여야합니다. 우리 프로젝트의 다른 클래스에 따라 달라질 수 있습니다 –
[C# 개체 별 값순 정렬 목록] (http://stackoverflow.com/questions/16649481/c-sharp-sorted-list-by-value-with-object) – nawfal