代码之家  ›  专栏  ›  技术社区  ›  Tim Schmelter

产量换算成VB.NET版

  •  3
  • Tim Schmelter  · 技术社区  · 15 年前

    首先,我必须假设我不太熟悉C#yield关键字及其函数。 “翻译”成英语的最好/最简单的方法是什么VB.NET? 尤其是我试图改变信仰 this code

    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
    
    5 回复  |  直到 15 年前
        1
  •  3
  •   Marc Gravell    15 年前

    自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);
        }
    }
    
        2
  •  3
  •   Tim Murphy    15 年前

    我是一个VB.NET版开发人员和下一个放弃将C代码从网络转换为VB.NET版. 我只是有一个C#库,用于将代码转储到所需的项目中。只有当我发现我需要定期/大量地开发代码时,我才会经历转换代码的痛苦VB.NET版.

        3
  •  3
  •   CoderDennis    13 年前

    Async CTP 包括对 Yield 在VB.NET版.

    看到了吗 Iterators in Visual Basic 有关用法的信息。

    现在呢 it's included in .NET 4.5 and VS 2012

        4
  •  1
  •   sloth    15 年前

    如果您真的想手工实现迭代器类,我建议您阅读 this chapter Jon Skeet的“c#深入”一书中,首先要了解c#编译器是如何处理这个小问题的 产量 关键字。

        5
  •  1
  •   MarkJ    15 年前

    nice article 由比尔麦卡锡在visualstudio杂志上的模仿 yield IEnumerable(Of T) IEnumerator(Of T)