English 中文(简体)
将C#代码改为VB。 NET
原标题:Translation of this C# code to VB.NET

我试图按照C#代码进行翻译。

public static class ObjectSetExtensions
{
    public static void AddObjects<T>(this ObjectSet<T> objectSet, IEnumerable<T> objects)
    {
         foreach (var item in objects)
         {
             objectSet.AddObject(item);
         }
    }
}

页: 1 NET:

Module ObjectSetExtensions
    <System.Runtime.CompilerServices.Extension()>
    Public Sub AddObjects(Of T)(ByVal objectSet As ObjectSet(Of T), ByVal objects As IEnumerable(Of T))
        For Each item In objects
            objectSet.AddObject(item)
        Next
    End Sub
End Module

但我犯了一个错误:

类型理由 T不能满足对等参数TEnt的等级限制。

我失踪了什么?

最佳回答

由于同样的原因,C#版本没有汇编。 应当:

public static void AddObjects<T>(this ObjectSet<T> objectSet,
                                 IEnumerable<T> objects)
    where T : class // Note this bit
{
     foreach (var item in objects)
     {
         objectSet.AddObject(item);
     }
}

VB版本是:

<Extension> _
Public Sub AddObjects(Of T As Class)(ByVal objectSet As ObjectSet(Of T), _
                                     ByVal objects As IEnumerable(Of T))
    Dim local As T
    For Each local In objects
        objectSet.AddObject(local)
    Next
End Sub

说明在VB版本中,限制是类型参数申报的一部分。 详情见。 MSDN

问题回答

如同你一样,它缺乏一个制约因素:

C#:
   where T : class

VB:
   (Of T as class)

你的译文没有任何错误。 这是最接近的VB。 相当于C#代码的网络。

根据<代码>的汇编错误 标的预计一般类型参数将限制在<代码><>>>>。 最初的C#例没有,但是如果是,并且从VB的问题中删除了。 互联网当量如下。

Public Sub AddObjects(Of T As Class)(ByVal objectSet As ObjectSet(Of T) ...

另一种改为VB。 NET:

Public NotInheritable Class ObjectSetExtensions

    Private Sub New()
    End Sub

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Sub AddObjects(Of T)(objectSet As ObjectSet(Of T), objects As IEnumerable(Of T))
        For Each item As var In objects
            objectSet.AddObject(item)
        Next
    End Sub

End Class




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签