将yield转换为VB.NET

首先,我必须假设我不熟悉C#yield关键字及其function。 将它“翻译”成VB.NET的最佳/最简单方法是什么? 特别是我试图将此代码转换为VB.NET,但我失败了:

yield return new MatchNode(++index, current.Value); 

我拥有的是:

 Imports System.Collections Imports System.Data.SqlTypes Imports System.Diagnostics.CodeAnalysis Imports System.Text.RegularExpressions Imports Microsoft.SqlServer.Server Class MatchNode Private _index As Integer Private _value As String Public Sub New(ByVal index As Integer, ByVal value As String) _index = index _value = value End Sub Public ReadOnly Property Index() As Integer Get Return _index End Get End Property Public ReadOnly Property Value() As String Get Return _value End Get End Property End Class Class MatchIterator Implements IEnumerable Private _regex As Regex Private _input As String Public Sub New(ByVal input As String, ByVal pattern As String) MyBase.New() _regex = New Regex(pattern, UserDefinedFunctions.Options) _input = input End Sub Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator Dim index As Integer = 0 Dim current As Match = Nothing While (current Is Nothing OrElse current.Success) If current Is Nothing Then current = _regex.Match(_input) Else current = current.NextMatch() End If If current.Success Then index += 1 'following should be a VB.Net yield' Return New MatchNode(index, current.Value) End If End While End Function End Class Partial Public Class UserDefinedFunctions  _ Public Shared Function RegexMatches(ByVal input As SqlChars, ByVal pattern As SqlString) As IEnumerable Return New MatchIterator(New String(input.Value), pattern.Value) End Function Public Shared Sub FillMatchRow(ByVal data As Object, ByRef index As SqlInt32, ByRef text As SqlChars) Dim node As MatchNode = CType(data, MatchNode) index = New SqlInt32(node.Index) text = New SqlChars(node.Value.ToCharArray) End Sub End Class 

由于VB.NET不提供迭代器块,因此您必须手动编写迭代器类,这非常痛苦。 我会尝试用C#手动编写(手动),所以你可以看到我的意思……就像这样:

 internal class MatchIterator : IEnumerable { private class MatchEnumerator : IEnumerator { int index = 0; private Match currentMatch; private MatchNode current; readonly Regex regex; readonly string input; public MatchEnumerator(Regex regex, string input) { this.regex = regex; this.input = input; } public object Current { get { return current; } } public void Reset() { throw new NotSupportedException(); } public bool MoveNext() { currentMatch = (currentMatch == null) ? regex.Match(input) : currentMatch.NextMatch(); if (currentMatch.Success) { current = new MatchNode(++index, currentMatch.Value); return true; } return false; } } private Regex _regex; private string _input; public MatchIterator(string input, string pattern) { _regex = new Regex(pattern, UserDefinedFunctions.Options); _input = input; } public IEnumerator GetEnumerator() { return new MatchEnumerator(_regex, _input); } } 

你必须问自己我真的要编辑这段代码吗? 如果答案是否定的,那么请不要打扰,将其保留为C#代码。

我是一名VB.NET开发人员,接下来就放弃了将网络上的C#代码转换为VB.NET。 我只是有一个C#库用于我将代码转储到所需的项目中。 只有当我发现我需要定期/大量开发代码时,我才会将其转换为VB.NET。

新的Async CTP包括对VB.NET中的Yield支持。

有关用法的信息,请参阅Visual Basic中的迭代器 。

现在它包含在.NET 4.5和VS 2012中 。

如果你真的真的想要手工实现迭代器类,我建议首先阅读Jon Skeet的“c#in depth” 这一章 ,以了解c#-compiler对这个小yield关键字的作用。

在Visual Studio杂志中,Bill McCarthy通过实现IEnumerable(Of T)IEnumerator(Of T)来模拟VB.NET中的yield , 这篇文章很精彩 。