将代码更改为.net 2.0

发布于 2024-09-26 19:52:39 字数 1281 浏览 0 评论 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 }, 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");
     }

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");
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界和平 2024-10-03 19:52:40

此行

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;
}

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;
}
遗弃M 2024-10-03 19:52:40

.net 3.5 不支持 OrderByDescending 是什么意思?确实如此。顺便问一下,Max(x => x.Length) 有什么问题吗?

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)?

谁的新欢旧爱 2024-10-03 19:52:40

在 .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

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

谁许谁一生繁华 2024-10-03 19:52:39

我怀疑你只是缺乏;

using System.Linq;

位于代码文件的顶部。不,切换到 .net 2 在这里没有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文