【问题标题】:Hide Class Library Enums from applications从应用程序中隐藏类库枚举
【发布时间】:2021-09-01 02:19:46
【问题描述】:

我在类库中使用公共枚举,该枚举在类库中的不同类中使用。我不希望它暴露给任何使用类库的应用程序。我尝试将其更改为 Private,但它说它必须驻留在一个类中才能做到这一点(我不能这样做,因为它被许多类使用)。如何“隐藏”它?

编辑:抱歉没有代码。这是类库中的一些代码(我知道它是 vb,但我认为 c# 知识将适用?)

Public Enum HttpMethod
    [Get]
    Post
    Put
End Enum

Public Class WebClientProcessor
    Public Function HttpAction(netquery As String, method As HttpMethod, Optional json As String = Nothing) As WebClientResponse
    ' Stuff here
    End Function
End Class

Public Class Main
    Public Sub DoStuff
        Dim wcp As New WebClientProcessor
        wcr = wcp.HttpAction(StringOp.UrlCombine(_baseSiteURL, _licenseEndpointsMap(RequestType), LicenseKey), HttpMethod.Get)
    End Sub
End Class

【问题讨论】:

  • 您能提供一些代码,以便我们有更清晰的上下文吗?谢谢
  • 使用internal
  • 你能解释一下这些模糊的句子吗:“Hide Class Library Enums from applications”以及“a Public Enum in a Class Library that is used in different classes在库中”和“不希望它暴露给任何使用类库的应用程序”和“将它(什么?类型或实例?)更改为私有",例如一些代码,好吗?
  • vb.net 中的FriendFriend Enum HttpMethod ... End Enum.
  • 不要把你的解决方案放在问题中。如果您不需要对该问题的答案,请删除该问题,否则添加解决方案的答案。

标签: vb.net enums class-library


【解决方案1】:

使用internal 修饰符。

内部类型或成员只能在同一程序集中的文件中访问。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal

【讨论】:

  • 这是一个完美的例子,说明您为什么不发送垃圾标签。现在有一个基于标签的完全合法的答案,但实际上与实际问题无关。
【解决方案2】:

我最终通过删除“公共”来解决它:

Enum HttpMethod
    [Get]
    Post
    Put
End Enum

【讨论】:

  • 我认为如果没有明确说明,枚举默认会公开。它会被视为 Internal\Friend 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
相关资源
最近更新 更多