.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");
}
答案 0 :(得分:2)
我怀疑你只是缺乏;
using System.Linq;
在代码文件的顶部。不,切换到.net 2对此没有帮助。
答案 1 :(得分:0)
你是什么意思.net 3.5不支持OrderByDescending
。确实如此。顺便说一下Max(x => x.Length)
出了什么问题?
答案 2 :(得分:0)
在.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
答案 3 :(得分:0)
这一行
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;
}