【问题标题】:Not accessible because it is Friend in Generics无法访问,因为它是泛型中的朋友
【发布时间】:2011-07-05 08:11:32
【问题描述】:

我试图让一个泛型方法在 VB.NET 中工作,但我无法获得接受泛型类型参数的签名。我只是得到“'t' is not accesble because it is "Friend"”。

我的方法签名是:

Public Shared Function GetOffset(ByVal coll As IEnumerable(Of t), ByVal offset As Integer, ByVal limit As Integer) As IEnumerable(Of t)

【问题讨论】:

    标签: .net asp.net vb.net generics


    【解决方案1】:

    据我所见,您还没有指定类型参数。不应该是:

    Public Shared Function GetOffset(Of t)(ByVal coll As IEnumerable(Of t), _
         ByVal offset As Integer, ByVal limit As Integer) As IEnumerable(Of t)
    

    请注意,通常这将是 T 而不是 t

    【讨论】:

    • 谢谢。 VB.NET 似乎比 C# 更冗长,因为我必须声明该类型参数 3 次。
    • @Echilon:不,你只是声明一次类型参数-然后你使用类型参数两次......就像在 C# 中一样:public static IEnumerable<T> GetOffset<T>(IEnumerable<T> coll, int offset, int limit) - 你会看到 <T> 仍然出现了 3 次。
    【解决方案2】:

    你在另一个程序集中有一个名为 t 的类吗?朋友意味着它只能在该程序集中访问。考虑将其公开。然后其他程序集也可以访问它。

    Public:可供所有引用此程序集的程序集访问。 受保护:所有继承自此类的类均可访问。 朋友:同一程序集中的所有类都可以访问。 Private:仅此类可访问。 受保护的朋友:此程序集中的所有类都可以访问,这些类也继承自此类。

    如果 t 意味着任何类,请使用 IEnumerable(Of T)(大写 T)。

    【讨论】:

    • 不,它只是一个 ASP.NET 网站。
    • 什么是 t?一类?还是通用参数?在后一种情况下,尝试 T。在前一种情况下,t 应该是公开的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2019-10-28
    • 2014-05-26
    • 2013-06-30
    • 2015-08-03
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多