与此C#linq类等效的VB.NET
伙计们,我正在尝试将一些东西从C#转换到VB.NET,我在VB.NET中找不到与C#的yield关键字等价的词。我意识到“yield”不是VB.NET的可转换关键字,所以请有人告诉我如何在VB.NET中实现这段代码。除了实现的GetEnumerator()函数外,我已将其全部转换。它只是一个实现CollectionBase和IEnumerable的类(使其具有LINQ价值):与此C#linq类等效的VB.NET,c#,vb.net,linq,ienumerable,C#,Vb.net,Linq,Ienumerable,伙计们,我正在尝试将一些东西从C#转换到VB.NET,我在VB.NET中找不到与C#的yield关键字等价的词。我意识到“yield”不是VB.NET的可转换关键字,所以请有人告诉我如何在VB.NET中实现这段代码。除了实现的GetEnumerator()函数外,我已将其全部转换。它只是一个实现CollectionBase和IEnumerable的类(使其具有LINQ价值): [Serializable()] 公共部分类客户:CollectionBase、System.Collections.G
[Serializable()]
公共部分类客户:CollectionBase、System.Collections.Generic.IEnumerable
{
public new System.Collections.Generic.IEnumerator GetEnumerator()
{
foreach(此列表中的BusinessLayer.Cust)
{
收益回报率;
}
}
公众客户()
{
}
公共客户(DataRowCollection datarows):this()
{
加载(数据行);
}
受保护的无效加载(DataRowCollection dataRows)
{
foreach(数据行中的数据行dr){
添加(新客户(dr));
}
}
公共客户本[int索引]{
获取{return(Customer)base.InnerList[index];}
set{base.InnerList[index]=value;}
}
公共整数添加(客户val)
{
返回base.InnerList.Add(val);
}
}
提前感谢您的帮助 因为不能使用
yield
关键字,所以必须以另一种方式实现GetEnumerator()。您可以做的是从CollectionBase
返回列表的枚举数。但是,由于这是一个IList
而不是IList
,因此您必须强制转换它(您可以使用Linq中的cast()
扩展方法进行转换)。然后,您的C#代码变为:
public IEnumerator<BusinessLayer.Customer> GetEnumerator()
{
return InnerList.Cast<BusinessLayer.Customer>().GetEnumerator();
}
剩下的代码应该直接翻译到VB.Net。因为不能使用yield
关键字,所以必须以另一种方式实现GetEnumerator()。您可以做的是从CollectionBase
返回列表的枚举数。但是,由于这是一个IList
而不是IList
,因此您必须强制转换它(您可以使用Linq中的cast()
扩展方法进行转换)。然后,您的C#代码变为:
public IEnumerator<BusinessLayer.Customer> GetEnumerator()
{
return InnerList.Cast<BusinessLayer.Customer>().GetEnumerator();
}
您的其余代码应该直接翻译到VB.Net。要么等待下一版本的VB.Net,要么在Visual Studio杂志上咨询Bill McCarthy。要么等待下一版本的VB.Net,要么在Visual Studio杂志上咨询Bill McCarthy。我知道您问这个问题已经有一段时间了,但是我遇到了同样的问题,在VB中没有“yield”关键字。这就是我发现的,并一直在用它来代替它。您可以使用泛型迭代器实现“yield”,下面是此类迭代器的代码:
Public Class GenericIterator(Of T)
Implements IEnumerable(Of T)
Implements IEnumerator(Of T)
Public Delegate Function MoveNextFunc(ByRef nextItem As T) As Boolean
Private _Current As T
Private _func As MoveNextFunc
Public Sub New(ByVal func As MoveNextFunc)
_func = func
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
Return _func(_Current)
End Function
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Static iBeenCalled As Int32
If (iBeenCalled = 0) AndAlso _
(Threading.Interlocked.Increment(iBeenCalled) = 1) Then
Return Me
Else
Return New GenericIterator(Of T)(_func)
End If
End Function
Public ReadOnly Property Current() As T Implements IEnumerator(Of T).Current
Get
Return _Current
End Get
End Property
Public Overridable Sub Reset() Implements IEnumerator.Reset
Throw New NotImplementedException("Iterator cannot be reset")
End Sub
Private Function IEnumerator_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return Me.GetEnumerator
End Function
Private ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
Get
Return Me.Current
End Get
End Property
Public Sub Dispose() Implements IDisposable.Dispose
' not implemented
End Sub
End Class
一旦有了这个类,您就可以实现“yield”,类似于在这个示例Zip扩展函数中的实现方式:
Public Module IEnumerableExtensions
<Extension()>
Public Function Zip(Of T1, T2)(ByVal left As IEnumerable(Of T1), ByVal right As IEnumerable(Of T2)) As IEnumerable(Of Pair(Of T1, T2))
Dim leftG As IEnumerator(Of T1) = left.Select(Function(x) x).GetEnumerator()
Dim rightG As IEnumerator(Of T2) = right.Select(Function(x) x).GetEnumerator()
Return New GenericIterator(Of Pair(Of T1, T2)) _
(Function(ByRef x) As Boolean
Dim canMove As Boolean = leftG.MoveNext() AndAlso rightG.MoveNext()
x = New Pair(Of T1, T2)(leftG.Current, rightG.Current)
Return canMove
End Function)
End Function
End Module
我希望这在某种程度上有助于你的转化努力!祝你好运 我知道你问这个问题已经有一段时间了,但是我遇到了同样的问题,在VB中没有“yield”关键字。这就是我发现的,并一直在用它来代替它。您可以使用泛型迭代器实现“yield”,下面是此类迭代器的代码:
Public Class GenericIterator(Of T)
Implements IEnumerable(Of T)
Implements IEnumerator(Of T)
Public Delegate Function MoveNextFunc(ByRef nextItem As T) As Boolean
Private _Current As T
Private _func As MoveNextFunc
Public Sub New(ByVal func As MoveNextFunc)
_func = func
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
Return _func(_Current)
End Function
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Static iBeenCalled As Int32
If (iBeenCalled = 0) AndAlso _
(Threading.Interlocked.Increment(iBeenCalled) = 1) Then
Return Me
Else
Return New GenericIterator(Of T)(_func)
End If
End Function
Public ReadOnly Property Current() As T Implements IEnumerator(Of T).Current
Get
Return _Current
End Get
End Property
Public Overridable Sub Reset() Implements IEnumerator.Reset
Throw New NotImplementedException("Iterator cannot be reset")
End Sub
Private Function IEnumerator_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return Me.GetEnumerator
End Function
Private ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
Get
Return Me.Current
End Get
End Property
Public Sub Dispose() Implements IDisposable.Dispose
' not implemented
End Sub
End Class
一旦有了这个类,您就可以实现“yield”,类似于在这个示例Zip扩展函数中的实现方式:
Public Module IEnumerableExtensions
<Extension()>
Public Function Zip(Of T1, T2)(ByVal left As IEnumerable(Of T1), ByVal right As IEnumerable(Of T2)) As IEnumerable(Of Pair(Of T1, T2))
Dim leftG As IEnumerator(Of T1) = left.Select(Function(x) x).GetEnumerator()
Dim rightG As IEnumerator(Of T2) = right.Select(Function(x) x).GetEnumerator()
Return New GenericIterator(Of Pair(Of T1, T2)) _
(Function(ByRef x) As Boolean
Dim canMove As Boolean = leftG.MoveNext() AndAlso rightG.MoveNext()
x = New Pair(Of T1, T2)(leftG.Current, rightG.Current)
Return canMove
End Function)
End Function
End Module
我希望这在某种程度上有助于你的转化努力!祝你好运 如果您告诉VB人员Yield关键字的作用,您可能会从他们那里得到更多帮助。对于我35年运营商的大部分时间来说,“收益”意味着一个线程将剩余的CPU时间交给调度程序,以便让任何其他挂起的线程运行。我想现在MS的意思不同了?(因为有一个简单的、长期存在的VB惯例)如果你告诉VB人员Yield关键字的作用,你可能会从他们那里得到更多的帮助。对于我35年运营商的大部分时间来说,“收益”意味着一个线程将剩余的CPU时间交给调度程序,以便让任何其他挂起的线程运行。我想现在MS的意思不同了?(因为有一个简单的、长期存在的VB惯例)我很欣赏这个答案。阿德里安的回答满足了我的需要,我很感激你的回答。阿德里安的回答满足了我的需要。