C# 将代码更改为.net 2.0

C# 将代码更改为.net 2.0,c#,.net-3.5,.net-2.0,C#,.net 3.5,.net 2.0,似乎.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 },

似乎.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");
     }
private static Dictionary imageFormatDecoders=new Dictionary()
{
{新字节[]{0x42,0x4D},解码位图},
{新字节[]{0x47,0x49,0x46,0x38,0x37,0x61},decodegf},
{新字节[]{0x47,0x49,0x46,0x38,0x39,0x61},decodegf},
{新字节[]{0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A},DecodePng},
{新字节[]{0xff,0xd8},DecodeJfif},
};
公共静态大小GetDimensions(BinaryReader BinaryReader)
{
int MaxMagicBytes长度=imageFormatDecoders.Keys.OrderByDescending(x=>x.Length).First().Length;
字节[]magicBytes=新字节[maxmagicBytes长度];
对于(int i=0;i
您认为.net 3.5不支持
OrderByDescending
是什么意思。是的。顺便问一下,
Max(x=>x.Length)
有什么问题?

在.NET3.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);
} 
Dictionary dict=new Dictionary();
dict[0]=2;
dict[1]=3;
foreach(dict.OrderByDescending(key=>key.Value)中的var项)
{
控制台。写入线(项。键);
Console.WriteLine(项值);
} 
输出

一,

三,

0

二,


我怀疑你只是缺乏

using System.Linq;
在代码文件的顶部。不,切换到.net 2在这里没有帮助。

这行

int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
只获取字典键中最长字节数组的长度。因此,只需在
imageFormatDecoders
中迭代您的项目,并记录最长的值,即类似以下内容(未测试):


嗯??OrderByDescending调用在.NET3.5中应该可以正常运行。。。但是,为什么您希望迁移到2.0来解决这个问题呢?你希望字节[].StartsWith
从何而来?@oded-使用directiveHi听起来像是一个缺失。谢谢,你能解释一下你想用什么吗?@Ata:
int-maxMagicBytesLength=imageFormatDecoders.Keys.Max(x=>x.Length)
。你错了,dot-net 2.0不允许我使用dot-net 3.5 component.WTF,你一开始说你在使用.NET3.5。我可以看出你现在已经改变了你的问题,但我必须承认我没有注意到这一点。嗨,谢谢你,你的方式是对的,但是我如何处理字节[]。在dot net 2.0中StartsWith?@Ata:将foreach循环移到for循环之外。在foreach循环中,创建一个比较第一个
kvPair.Key.Length
项的循环。
int maxMagicBytesLength = 0;
foreach (byte[] magicBytes in imageFormatDecoders.Keys) {
    if (magicBytes.Length > maxMagicBytesLength)
        maxMagicBytesLength = magicBytes.Length;
}