VB.NET 中“yield return”的等效语法是什么?

发布于 2024-09-02 15:37:47 字数 11197 浏览 7 评论 0原文

使用下面的 C# 代码,您将如何在 Visual Basic 中编写它?它想表达什么?

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Microsoft.LiveLabs.Pivot
{
    /// <summary>
    ///     Tile Builder class
    /// </summary>
    public static class TileBuilder
    {
        /// <summary>
        ///     Specifies which images are required in the images array used in CreateTile
        ///     according to the Morton fractal pattern used by Seadragon.
        /// </summary>
        /// <remarks>
        ///     Usage of this and CreateTile are kind of tricky. Here's an example:
        ///     Say you have a results set that is a collection of items like so: { item1, item2, ..., item100 }
        ///     Since Seadragon expects the tiles to be laid out in a Morton pattern,
        ///     level 6 will look like the following:
        ///
        ///     --------------------------
        ///     |0   1   4   5  | 16  17...
        ///     |2   3   6   7  | 18  19
        ///     |8   9   12  13 | 24  25
        ///     |10  11  14  15 | 26  27
        ///     |-----------------------
        ///     |32  33  36  37 | 48  49
        ///     |34  35  38  39 | 50  51...
        ///     |.          .
        ///     |.          .
        ///      .          .
        ///
        ///      Each tile at level 6 is 4x4, so the dashes represent tile boundaries. Now, say
        ///      you want to build 0,0. You need the images on that tile. The ids 0, 1, 4, 5...
        ///      represent the ids in your result set, { item1, item2, ..., item100 }. Calling
        ///      this method tells you the ids to use for a given tile. You then must retrieve
        ///      the necessary images out the result set, and supply them in the order this
        ///      method gave you to CreateTile. This will result in a correctly built tile
        ///      that Seadragon can use.
        /// </remarks>
        /// <param name="imageCount">Number of images in the full set.</param>
        /// <param name="level">The level to which each image will be downsampled.</param>
        /// <param name="row">The row number which specifies what images to render.</param>
        /// <param name="column">The row number which specifies what images to render.</param>
        /// <param name="tileSize">The size of the tile to return.</param>
        public static IEnumerable<int> GetTileIds(
          int imageCount,
          int level,
          int row,
          int column,
          int tileSize)
        {
            // Calculate upper-left hand corner of tile in image space (1 unit = 1 image)
            int levelSize = (int)Math.Pow(2, level);
            int imagePerSide = tileSize / levelSize;
            int xOffset = row * imagePerSide;
            int yOffset = column * imagePerSide;

            if (imagePerSide <= 0)
            {
                throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
            }

            // Loop through x and y in image space, starting at the upper-left
            // hand corner of the tile. Find all ids on the given tile.
            for (int x = 0; x < imagePerSide; x++)
            {
                for (int y = 0; y < imagePerSide; y++)
                {
                    int n = XYToMorton(x + xOffset, y + yOffset);
                    if (n < imageCount)
                    {
                        yield return n;
                    }
                }
            }
        }

        /// <summary>
        ///     Create a tile for a collection according to the Morton fractal
        ///     pattern used by Seadragon.
        /// </summary>
        /// <remarks>
        ///     See GetTileIds for more information.
        /// </remarks>
        /// <param name="imageCount">The total number of images in the collection.</param>
        /// <param name="images">Jpeg images to render on this tile.
        ///     If this is null, a blank tile will be returned.
        ///     See GetTileIds remarks for more information.</param>
        /// <param name="level">The level to which each image will be downsampled.</param>
        /// <param name="row">The row number which specifies what images to render.</param>
        /// <param name="column">The row number which specifies what images to render.</param>
        /// <param name="tileSize">The size of the tile to return.</param>
        /// <param name="output">The stream to use to output the result.</param>
        public static void CreateTile(
          int imageCount,
          IEnumerable<ImageBag> images,
          int level,
          int row,
          int column,
          int tileSize,
          string fileType,
          Stream output)
        {
            // Calculate upper-left hand corner of tile in image space (1 unit = 1 image).
            int levelSize = (int)Math.Pow(2, level);
            int imagePerSide = tileSize / levelSize;
            int xOffset = row * imagePerSide;
            int yOffset = column * imagePerSide;

            if (imagePerSide <= 0)
            {
                throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
            }

            if (output == null)
            {
                throw new ArgumentNullException("The given output stream is null.");
            }

            // Create the tile.
            WriteableBitmap outputBitmap = new WriteableBitmap(
              tileSize,
              tileSize,
              96,
              96,
              PixelFormats.Bgr24,
              null);

            // If images is null, return a blank tile.
            if (images != null)
            {
                // Loop through the tile in relative x and y image-space.
                IEnumerator<ImageBag> imageEnumerator = images.GetEnumerator();
                for (int x = 0; x < imagePerSide; x++)
                {
                    for (int y = 0; y < imagePerSide; y++)
                    {
                        // Convert to Morton id-space from the absolute image-space (to get absolute, add offsets).
                        int n = XYToMorton(x + xOffset, y + yOffset);
                        if (n < imageCount)
                        {
                            if (imageEnumerator.MoveNext())
                            {
                                if (imageEnumerator.Current == null)
                                {
                                    continue;
                                }

                                // Compute the pixel location
                                int locX = levelSize * x;
                                int locY = levelSize * y;

                                int width = 0;
                                int height = 0;
                                imageEnumerator.Current.ImageSize(out width, out height);
                                MemoryStream imageStream = new MemoryStream(imageEnumerator.Current.ImageData);

                                // Determine the largest tile size to the nearest power of two for
                                // this image: 2^ceil(lg max(width, height)).
                                double maxTileSize = Math.Pow(2, Math.Ceiling(Math.Log(Math.Max(width, height), 2)));

                                // Downsample to the correct size and decompress the image. The correct size
                                // is total dimenion of the image * level size / max tile size required for
                                // total width. Think of this as dividing the dimensions by two foreach
                                // levels, starting at the max tile size, and going up to the current
                                // tile size
                                TransformedBitmap downsampledImage = JpegDecoder.DownsampleJpeg(
                                    imageStream,
                                    Math.Ceiling(width * levelSize / maxTileSize),
                                    Math.Ceiling(height * levelSize / maxTileSize));

                                // Copy the pixels to a buffer and then write them to the
                                // appropriate region on the output image.
                                int stride = (downsampledImage.PixelWidth * downsampledImage.Format.BitsPerPixel + 7) / 8;
                                byte[] buffer = new byte[stride * downsampledImage.PixelHeight];
                                downsampledImage.CopyPixels(buffer, stride, 0);

                                Int32Rect outputRect = new Int32Rect(locX, locY, downsampledImage.PixelWidth, downsampledImage.PixelHeight);
                                outputBitmap.WritePixels(outputRect, buffer, stride, 0);
                            }
                            else
                            {
                                // We should render the image, but we're done with our list.
                                // So, exit both loops.
                                x = imagePerSide;
                                y = imagePerSide;
                            }
                        }
                        else
                        {
                            // Since n is monotonic wrt y, we know y has gone too far down, so
                            // we can reset it.
                            y = imagePerSide;
                        }
                    }
                }
            }

            // Write the output
            BitmapFrame outputFrame = BitmapFrame.Create(outputBitmap);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(outputFrame);
            encoder.Save(output);
        }

        /// <summary>
        ///     Converts an x and y to a Morton number
        /// </summary>
        /// <param name="x">x location to convert.</param>
        /// <param name="y">y location to convert.</param>
        /// <returns>Returns the morton number which corresponds to the
        ///          given x and y coordinates.</returns>
        private static int XYToMorton(int x, int y)
        {
            const uint BITS_PER_BYTE = 8;
            const uint BIT_PAIRS = sizeof(int) * BITS_PER_BYTE / 2;

            int morton = 0;
            for (int i = 0; i < BIT_PAIRS; i++)
            {
                morton |= (x & 1) << (i * 2);
                morton |= (y & 1) << ((i * 2) + 1);
                x >>= 1;
                y >>= 1;
            }

            return morton;
        }
    }
}

Using the C# code below, how would you write it in Visual Basic? What is it trying to say?

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Microsoft.LiveLabs.Pivot
{
    /// <summary>
    ///     Tile Builder class
    /// </summary>
    public static class TileBuilder
    {
        /// <summary>
        ///     Specifies which images are required in the images array used in CreateTile
        ///     according to the Morton fractal pattern used by Seadragon.
        /// </summary>
        /// <remarks>
        ///     Usage of this and CreateTile are kind of tricky. Here's an example:
        ///     Say you have a results set that is a collection of items like so: { item1, item2, ..., item100 }
        ///     Since Seadragon expects the tiles to be laid out in a Morton pattern,
        ///     level 6 will look like the following:
        ///
        ///     --------------------------
        ///     |0   1   4   5  | 16  17...
        ///     |2   3   6   7  | 18  19
        ///     |8   9   12  13 | 24  25
        ///     |10  11  14  15 | 26  27
        ///     |-----------------------
        ///     |32  33  36  37 | 48  49
        ///     |34  35  38  39 | 50  51...
        ///     |.          .
        ///     |.          .
        ///      .          .
        ///
        ///      Each tile at level 6 is 4x4, so the dashes represent tile boundaries. Now, say
        ///      you want to build 0,0. You need the images on that tile. The ids 0, 1, 4, 5...
        ///      represent the ids in your result set, { item1, item2, ..., item100 }. Calling
        ///      this method tells you the ids to use for a given tile. You then must retrieve
        ///      the necessary images out the result set, and supply them in the order this
        ///      method gave you to CreateTile. This will result in a correctly built tile
        ///      that Seadragon can use.
        /// </remarks>
        /// <param name="imageCount">Number of images in the full set.</param>
        /// <param name="level">The level to which each image will be downsampled.</param>
        /// <param name="row">The row number which specifies what images to render.</param>
        /// <param name="column">The row number which specifies what images to render.</param>
        /// <param name="tileSize">The size of the tile to return.</param>
        public static IEnumerable<int> GetTileIds(
          int imageCount,
          int level,
          int row,
          int column,
          int tileSize)
        {
            // Calculate upper-left hand corner of tile in image space (1 unit = 1 image)
            int levelSize = (int)Math.Pow(2, level);
            int imagePerSide = tileSize / levelSize;
            int xOffset = row * imagePerSide;
            int yOffset = column * imagePerSide;

            if (imagePerSide <= 0)
            {
                throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
            }

            // Loop through x and y in image space, starting at the upper-left
            // hand corner of the tile. Find all ids on the given tile.
            for (int x = 0; x < imagePerSide; x++)
            {
                for (int y = 0; y < imagePerSide; y++)
                {
                    int n = XYToMorton(x + xOffset, y + yOffset);
                    if (n < imageCount)
                    {
                        yield return n;
                    }
                }
            }
        }

        /// <summary>
        ///     Create a tile for a collection according to the Morton fractal
        ///     pattern used by Seadragon.
        /// </summary>
        /// <remarks>
        ///     See GetTileIds for more information.
        /// </remarks>
        /// <param name="imageCount">The total number of images in the collection.</param>
        /// <param name="images">Jpeg images to render on this tile.
        ///     If this is null, a blank tile will be returned.
        ///     See GetTileIds remarks for more information.</param>
        /// <param name="level">The level to which each image will be downsampled.</param>
        /// <param name="row">The row number which specifies what images to render.</param>
        /// <param name="column">The row number which specifies what images to render.</param>
        /// <param name="tileSize">The size of the tile to return.</param>
        /// <param name="output">The stream to use to output the result.</param>
        public static void CreateTile(
          int imageCount,
          IEnumerable<ImageBag> images,
          int level,
          int row,
          int column,
          int tileSize,
          string fileType,
          Stream output)
        {
            // Calculate upper-left hand corner of tile in image space (1 unit = 1 image).
            int levelSize = (int)Math.Pow(2, level);
            int imagePerSide = tileSize / levelSize;
            int xOffset = row * imagePerSide;
            int yOffset = column * imagePerSide;

            if (imagePerSide <= 0)
            {
                throw new ArgumentOutOfRangeException("Level is greater than the maximum depth allowed by the tile size, or the tile size is 0.");
            }

            if (output == null)
            {
                throw new ArgumentNullException("The given output stream is null.");
            }

            // Create the tile.
            WriteableBitmap outputBitmap = new WriteableBitmap(
              tileSize,
              tileSize,
              96,
              96,
              PixelFormats.Bgr24,
              null);

            // If images is null, return a blank tile.
            if (images != null)
            {
                // Loop through the tile in relative x and y image-space.
                IEnumerator<ImageBag> imageEnumerator = images.GetEnumerator();
                for (int x = 0; x < imagePerSide; x++)
                {
                    for (int y = 0; y < imagePerSide; y++)
                    {
                        // Convert to Morton id-space from the absolute image-space (to get absolute, add offsets).
                        int n = XYToMorton(x + xOffset, y + yOffset);
                        if (n < imageCount)
                        {
                            if (imageEnumerator.MoveNext())
                            {
                                if (imageEnumerator.Current == null)
                                {
                                    continue;
                                }

                                // Compute the pixel location
                                int locX = levelSize * x;
                                int locY = levelSize * y;

                                int width = 0;
                                int height = 0;
                                imageEnumerator.Current.ImageSize(out width, out height);
                                MemoryStream imageStream = new MemoryStream(imageEnumerator.Current.ImageData);

                                // Determine the largest tile size to the nearest power of two for
                                // this image: 2^ceil(lg max(width, height)).
                                double maxTileSize = Math.Pow(2, Math.Ceiling(Math.Log(Math.Max(width, height), 2)));

                                // Downsample to the correct size and decompress the image. The correct size
                                // is total dimenion of the image * level size / max tile size required for
                                // total width. Think of this as dividing the dimensions by two foreach
                                // levels, starting at the max tile size, and going up to the current
                                // tile size
                                TransformedBitmap downsampledImage = JpegDecoder.DownsampleJpeg(
                                    imageStream,
                                    Math.Ceiling(width * levelSize / maxTileSize),
                                    Math.Ceiling(height * levelSize / maxTileSize));

                                // Copy the pixels to a buffer and then write them to the
                                // appropriate region on the output image.
                                int stride = (downsampledImage.PixelWidth * downsampledImage.Format.BitsPerPixel + 7) / 8;
                                byte[] buffer = new byte[stride * downsampledImage.PixelHeight];
                                downsampledImage.CopyPixels(buffer, stride, 0);

                                Int32Rect outputRect = new Int32Rect(locX, locY, downsampledImage.PixelWidth, downsampledImage.PixelHeight);
                                outputBitmap.WritePixels(outputRect, buffer, stride, 0);
                            }
                            else
                            {
                                // We should render the image, but we're done with our list.
                                // So, exit both loops.
                                x = imagePerSide;
                                y = imagePerSide;
                            }
                        }
                        else
                        {
                            // Since n is monotonic wrt y, we know y has gone too far down, so
                            // we can reset it.
                            y = imagePerSide;
                        }
                    }
                }
            }

            // Write the output
            BitmapFrame outputFrame = BitmapFrame.Create(outputBitmap);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(outputFrame);
            encoder.Save(output);
        }

        /// <summary>
        ///     Converts an x and y to a Morton number
        /// </summary>
        /// <param name="x">x location to convert.</param>
        /// <param name="y">y location to convert.</param>
        /// <returns>Returns the morton number which corresponds to the
        ///          given x and y coordinates.</returns>
        private static int XYToMorton(int x, int y)
        {
            const uint BITS_PER_BYTE = 8;
            const uint BIT_PAIRS = sizeof(int) * BITS_PER_BYTE / 2;

            int morton = 0;
            for (int i = 0; i < BIT_PAIRS; i++)
            {
                morton |= (x & 1) << (i * 2);
                morton |= (y & 1) << ((i * 2) + 1);
                x >>= 1;
                y >>= 1;
            }

            return morton;
        }
    }
}

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

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

发布评论

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

评论(6

山川志 2024-09-09 15:37:47

没有。时期。除非您要编写自己的状态机,否则没有快速解决方案。请参阅博客文章VB.NET 的“收益回报”

对于那些关心实际生成内容的人(是的,我喜欢 C# 预编译器和编译器:)):

尝试编译它并使用 .NET Reflector 或其他东西:

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();

        foreach(var result in foo.Bar())
        {
            Console.WriteLine(result);
        }

        Console.ReadLine();
    }
}

class Foo
{
    public IEnumerable<char> Bar()
    {
        const char start = 'a';

        for(int x = 0;x < 26;x++)
        {
            yield return (char)(start + x);
        }
    }
}

我不会复制结果,它很大。但仔细看看,你会发现这个问题并不容易解决。

There is none. Period. Unless you are going to write your own state machine, there is no quick fix for this. See the blog post VB.NET's "yield return" .

For those who care about what is actually generated (yes, I love the C# precompiler and compiler :) ):

Try compiling this and take a look at the generated code with .NET Reflector or something:

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();

        foreach(var result in foo.Bar())
        {
            Console.WriteLine(result);
        }

        Console.ReadLine();
    }
}

class Foo
{
    public IEnumerable<char> Bar()
    {
        const char start = 'a';

        for(int x = 0;x < 26;x++)
        {
            yield return (char)(start + x);
        }
    }
}

I'm not going to copy the result, it's huge. But do take a look, you'll see that it isn't simple to solve.

你的背包 2024-09-09 15:37:47

包含 yield return/yield break 语句的代码块称为 迭代器。 VB中没有这个功能。当然,可以“翻译”该代码,但不是很简单......您需要创建一个实现 IEnumerator(Of Integer) 的自定义类来重现完全相同的行为:

  • 初始化它使用原始函数的参数
  • 通过将循环变量存储在私有字段中,在 MoveNext 方法中实现“循环”逻辑。此方法还应该设置一个字段来保存当前值(原始函数中的n
  • Current属性返回当前值
  • 重置中的循环变量Reset 方法
  • Dispose 方法中不执行任何操作

这是可行的,但说实话,这很痛苦...

另一个更简单的选择是填充 List(Of Integer)List(Of Integer) code> 与结果并返回该列表:只需将 yield return n 替换为 list.Add(n)

A block of code with yield return/yield break statements is called an iterator. This feature doesn't exist in VB. Of course, it is possible to "translate" that code, but it's not very straightforward... you need to create a custom class implementing IEnumerator(Of Integer) to reproduce exactly the same behavior :

  • Initialize it with the parameters of the original function
  • Implement the "loop" logic in the MoveNext method, by storing the loop variables in private fields. This method should also set a field to hold the current value (n in the original function)
  • Return the current value from the Current property
  • Reset the loop variables in the Reset method
  • Do nothing in the Dispose method

It's doable, but honestly, it's a pain...

Another, easier option would be to fill a List(Of Integer) with the results and return that list : just replace yield return n with list.Add(n)

琉璃梦幻 2024-09-09 15:37:47

VB 中没有与 yield 完全相同的副本。最好的选择是创建正在 yielded 的集合,并将其整个返回以供调用代码进行迭代。这与您给出的代码不太完全相同,但它应该具有相同的整体效果。 yield 本质上只是语法糖,用于一次将集合的一部分返回到 foreach 循环,以优化运行时间并允许早期突破(如果您决定不这样做)毕竟不需要枚举所有内容)。

There is no exact duplicate of yield in VB. Your best bet would be to create the collection that is being yielded and return it whole for the calling code to iterate over. This isn't quite exactly the same as the code you've given, but it should have the same overall effect. yield is essentially just syntactic sugar for returning parts of a collection to an foreach loop one at a time to optimize run time and allow for early breakout (if you decide you don't need to enumerate over everything after all).

送君千里 2024-09-09 15:37:47

没有。不过,您可以创建一个。
创建一个迭代器类。
执行此操作的几个步骤:

  1. 创建一个基本上是迭代中的一个步骤的函数。该函数应采用每次计算所需的任何参数,并返回下一次计算的参数。 (要实现yieldbreak以及yieldreturn,这个函数还需要能够返回一些标记结束的布尔值)
  2. 创建一个实现IEnumerator的类。 Current 属性应返回最新的计算值,Reset 应将所有值重置为初始值,MoveNext 函数应计算下一个值,如果计算函数返回 false,则本身返回 false。

为了获得额外的积分,请使该类具有通用性并能够与委托赋予它的任何功能一起使用。
您可以稍微简化一下,使 MoveNext 成为计算函数本身,但这样可以轻松地从同一类实现中创建不同的迭代器。

There isn't. You can however, create one.
Create an iterator class.
A few steps to doing this:

  1. Create a function that is basically a step in the iteration. The function should take whatever arguments each computation needs, and return the arguments for the next computation. (to implement yield break aswell as yield return this function would need to also be able to return some boolean marking the end)
  2. Create a class that implements IEnumerator. The Current property should return the latest computed value, the Reset should reset all the values to their initial ones, and the MoveNext function should compute the next value, and if the computing function returns a false, return a false itself.

For extra credit, make the class generic and capable to work with any function given to it by a delegate.
You can simplify this a bit and make MoveNext be the calculating function itself, but this way it's easy to create different iterators out of the same class implementation.

滿滿的愛 2024-09-09 15:37:47

VB.NET 中没有与 yield return 等效的函数,但可以对其进行模拟。

请参阅 Stack Overflow 问题VB.NET 中的 Yield 及其答案。

也许Visual Studio 杂志文章在 VB Now 中使用迭代器将会有所帮助。

There is no equivalent to yield return in VB.NET, but it can be emulated.

See Stack Overflow question Yield in VB.NET and its answers.

Perhaps the Visual Studio Magazine article Use Iterators in VB Now will help.

无戏配角 2024-09-09 15:37:47

C#:

yield return scriptRef

VB.NET:

Return New List(Of ScriptReference) From {scriptRef}

C#:

yield return scriptRef

VB.NET:

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