枚举值的通用数组?
我目前正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于此代码的一些注释:
TypeFromType()
而不是TypeToType()
命名约定,因为它将类型放置在包含它们的变量附近,而不是相反。Some notes on this code:
TypeFromType()
rather thanTypeToType()
naming convention, because it places the types near the variable that contain them rather than the reverse.