Изменить код на .net 2.0 - PullRequest
       34

Изменить код на .net 2.0

1 голос
/ 08 октября 2010

Кажется, что .net 2.0 не поддерживает OrderByDescending для ключей словаря, как я могу изменить этот код на .net 2.0

    private static Dictionary<byte[], Func<BinaryReader, Size>> imageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>>()
 {
     { new byte[]{ 0x42, 0x4D }, DecodeBitmap},
     { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
     { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
     { new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
     { new byte[]{ 0xff, 0xd8 }, DecodeJfif },
 };


public static Size GetDimensions(BinaryReader binaryReader)
     {
         int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
         byte[] magicBytes = new byte[maxMagicBytesLength];
         for (int i = 0; i < maxMagicBytesLength; i += 1)
         {
             magicBytes[i] = binaryReader.ReadByte();
             foreach (var kvPair in imageFormatDecoders)
             {
                 if (magicBytes.StartsWith(kvPair.Key))
                 {
                     return kvPair.Value(binaryReader);
                 }
             }
         }
         throw new ArgumentException(errorMessage, "binaryReader");
     }

Ответы [ 4 ]

2 голосов
/ 08 октября 2010

Я подозреваю, что вам просто не хватает;

using System.Linq;

В верхней части файла кода. И нет, переход на .net 2 здесь не поможет.

0 голосов
/ 08 октября 2010

Эта строка

int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;

просто получает длину самого длинного байтового массива в ключах вашего словаря.Так что просто переберите свои элементы в imageFormatDecoders и запишите самое длинное значение, то есть что-то вроде этого (не проверено):

int maxMagicBytesLength = 0;
foreach (byte[] magicBytes in imageFormatDecoders.Keys) {
    if (magicBytes.Length > maxMagicBytesLength)
        maxMagicBytesLength = magicBytes.Length;
}
0 голосов
/ 08 октября 2010

Что с этим не так, в .Net 3.5?

Dictionary<int, int> dict = new Dictionary<int, int>();
dict[0] = 2;
dict[1] = 3;

foreach (var item in dict.OrderByDescending(key => key.Value))
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
} 

выход

1

3

0

2

0 голосов
/ 08 октября 2010

Что вы имеете в виду, что .net 3.5 не поддерживает OrderByDescending. Оно делает. И, кстати, что не так с Max(x => x.Length)?

...