首先我必须假设我不熟悉C#yield关键字及其功能。 将它“翻译”成VB.NET的最佳/最简单的方法是什么? 特别是我试图将this code转换为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
<SqlFunction(FillRowMethodName:="FillMatchRow", TableDefinition:="[Index] int,[Text] nvarchar(max)")> _
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
答案 0 :(得分:3)
由于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);
}
}
答案 1 :(得分:3)
你必须问自己我真的要编辑这段代码吗?如果答案是否定的,那么请不要打扰,将其保留为C#代码。
我是一名VB.NET开发人员,接下来就放弃了将网络上的C#代码转换为VB.NET。我只是有一个C#库用于我将代码转储到的所需项目。只有当我发现我需要定期/大量开发代码时,我才会将其转换为VB.NET。
答案 2 :(得分:3)
新的Async CTP包括对VB.NET中Yield
的支持。
有关使用情况的信息,请参阅Iterators in Visual Basic。
答案 3 :(得分:1)
如果你真的,真的想手动实现迭代器类,我建议首先阅读Jon Skeet的“{深入”的this chapter,以了解c#-compiler对这个小的 yield 关键字。
答案 4 :(得分:1)
Bill McCacey在Visual Studio杂志中通过实现yield
和IEnumerable(Of T)
在VB.NET中模拟IEnumerator(Of T)
,这是nice article。