枚举值的通用数组?

发布于 2024-07-17 07:26:12 字数 544 浏览 5 评论 0原文

我目前正在 VB.NET 中编写一个辅助函数,将枚举值数组转换为 CSV,但我遇到了一些困难......

我不确定如果我想让它足够通用来处理我传递给它的任何枚举。

这就是我到目前为止所得到的:

Public Shared Function EnumArrayToCSV(ByVal values() As System.Enum) As String
    Dim result As Generic.List(Of String) = New Generic.List(Of String)

    For i As Integer = 0 To values.GetUpperBound(0)
        result.Add(Convert.ToInt32(values(i)))
    Next i

    Return String.Join(",", result.ToArray)
End Function

我意识到这个争论是不正确的,因为我正在使用一组枚举。 理想情况下,我希望使用一组通用的枚举值。

有人可以帮忙吗?

I'm currently writing a helper function in VB.NET to convert an array of enum values to a CSV, but I'm encounting a few difficulties....

I'm not sure what type of arguement my function should take if I want to make it generic enough to handle any enum I pass into it.

This is what I've got so far:

Public Shared Function EnumArrayToCSV(ByVal values() As System.Enum) As String
    Dim result As Generic.List(Of String) = New Generic.List(Of String)

    For i As Integer = 0 To values.GetUpperBound(0)
        result.Add(Convert.ToInt32(values(i)))
    Next i

    Return String.Join(",", result.ToArray)
End Function

I realise that the arguement is incorrect as I am working with an array of enums. Ideally, I'd like to be working with a generic set of enum values.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还给你自由 2024-07-24 07:26:12
Public Shared Function CSVFromEnumValues(Of T)(ByVal values As IEnumerable(Of T)) As String
    Dim sb As New StringBuilder()

    Dim delimiter As String = ""
    For Each item As T In values
        sb.Append(delimiter).Append(Convert.ToInt32(item).ToString())
        delimiter = ","
    Next item

    return sb.ToString()
End Function

关于此代码的一些注释:

  • 我开始更喜欢 TypeFromType() 而不是 TypeToType() 命名约定,因为它将类型放置在包含它们的变量附近,而不是相反。
  • 此代码将接受一个数组。 它还接受列表或您能想到的大多数其他集合。 这是一件好事——它使您的代码更加强大和灵活。
  • 此代码不限于枚举值,但枚举值只是一种类型。 这些值本身没有什么特别的。 这其实并不是一件坏事。 它仍然适用于枚举值,因此您可以很好地使用它。 它适用于其他项目并不一定是一个缺陷。
Public Shared Function CSVFromEnumValues(Of T)(ByVal values As IEnumerable(Of T)) As String
    Dim sb As New StringBuilder()

    Dim delimiter As String = ""
    For Each item As T In values
        sb.Append(delimiter).Append(Convert.ToInt32(item).ToString())
        delimiter = ","
    Next item

    return sb.ToString()
End Function

Some notes on this code:

  • I have come to prefer the TypeFromType() rather than TypeToType() naming convention, because it places the types near the variable that contain them rather than the reverse.
  • This code will accept an array. It will also accept a list or most any other collection you can think of. This is a good thing- it makes your code more powerful and flexible.
  • This code is not restricted to enum values, but enum values are just a type. There's nothing special about the values themsevles. This isn't really a bad thing. It still works for enum values, so you can use it just fine. That it works for other items isn't necessarily a flaw.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文