views:

81

answers:

4

hi

It seem that .net 2.0 do not support OrderByDescending for dictionary keys , how can I change this code to .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");
     }
A: 

What do you mean that .net 3.5 does not support OrderByDescending. It does. And by the way what is wrong with Max(x => x.Length)?

klausbyskov
Hi, thank you , can you explain what you want to use for this ?
Ata
@Ata: `int maxMagicBytesLength = imageFormatDecoders.Keys.Max(x => x.Length)`.
klausbyskov
Does .net support this ?
Ata
@Ata, Yes, if you use Linq.
klausbyskov
You are wrong , dot net 2.0 do not allow me to use dot net 3.5 component .
Ata
WTF, you started by saying that you were using .net 3.5. I can see that you have changed your question now, but I must admit that I didn't notice that.
klausbyskov
sorry me ! you are right .
Ata
A: 

What's wrong with this, in .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);
} 

Output

1

3

0

2

Steve Townsend
+2  A: 

I suspect you simply lack;

using System.Linq;

At the top of the code file. And no, switching to .net 2 won't help here.

Marc Gravell
That makes sense.
Steve Townsend
A: 

This line

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

just gets the length of the longest byte array in the keys of your dictionary. So just iterate through your items in imageFormatDecoders and record the longest value, i.e., something like this (untested):

int maxMagicBytesLength = 0;
foreach (byte[] magicBytes in imageFormatDecoders.Keys) {
    if (magicBytes.Length > maxMagicBytesLength)
        maxMagicBytesLength = magicBytes.Length;
}
Heinzi
Hi, thank you , you are in right way , but how can I handle byte[].StartsWith in dot net 2.0 ?
Ata
@Ata: Move the foreach loop outside the for loop. Inside the foreach loop, make a loop that compares the first `kvPair.Key.Length` items.
Heinzi