How to convert SortedList to SortedList <>

Due to the existing structure that I use, a method call returns a SortedList object. Since I wrote the other side of this call, I know that this is actually a SortedList. Although I can continue to work with SortedList, using generic could improve my meaning. So how do you change a non-generic SortedList to a properly typed generic SortedList?

The background in this case is that the call is a remote procedure call using SoapFormatter. SoapFormatter does not implement generics (thanks, Microsoft). I cannot change the formatter since some non-networks also use other method calls for the service.

I want my proxy request to look like this:

public SortedList<string, long> GetList(string parameter)
{
    return _service.GetList(parameter);
}

GetList - SoapFormatter:

public SortedList GetList(string parameter);
+5
2

:

static SortedList<TKey,TValue> StronglyType<TKey,TValue>(SortedList list) {
    var retval = new SortedList<TKey,TValue>(list.Count);
    for(int i=0; i<list.Count; i++) 
        retval.Add((TKey)list.GetKey(i), (TValue)list.GetByIndex(i));
    return retval;
}

foreach(DictionaryEntry entry in list) - , DictionaryEntry ( TKey/TValue).

: 100 , 1000 1000 .

+4

, SortedList SortedList<T>, T.

, SortedList<T> .

+6

All Articles