组合扩展方法

发布于 2024-08-04 03:54:17 字数 1635 浏览 8 评论 0原文

我正在尝试编写 2 个扩展方法来处理 Enum 类型。一种是使用描述属性为枚举选项提供更好的解释,第二种方法是列出枚举选项及其描述以在选择列表或某种集合中使用。

您可以在这里阅读我到目前为止的代码:

    <Extension()> _
    Public Function ToDescriptionString(ByVal en As System.Enum) As String

        Dim type As Type = en.GetType
        Dim entries() As String = en.ToString().Split(","c)
        Dim description(entries.Length) As String

        For i = 0 To entries.Length - 1
            Dim fieldInfo = type.GetField(entries(i).Trim())
            Dim attributes() = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())

            description(i) = If(attributes.Length > 0, attributes(0).Description, entries(i).Trim())
        Next

        Return String.Join(", ", description)

    End Function

    <Extension()> _
    Public Function ToListFirstTry(ByVal en As System.Enum) As IEnumerable

        Dim type As Type = en.GetType

        Dim items = From item In System.Enum.GetValues(type) _
           Select New With {.Value = item, .Text = item.ToDescriptionString}

        Return items

    End Function

    <Extension()> _
    Public Function ToListSecondTry(ByVal en As System.Enum) As IEnumerable

        Dim list As New Dictionary(Of Integer, String)
        Dim enumValues As Array = System.Enum.GetValues(en.GetType)

        For Each value In enumValues
            list.Add(value, value.ToDescriptionString)
        Next

        Return list

    End Function

所以我的问题是两种扩展方法不能很好地协同工作。将枚举选项转换为可枚举的方法不能使用扩展方法来获取描述。

我找到了各种各样的例子来实现两者之一,但从未将它们结合在一起。我做错了什么?我对这些新的 .NET 3.5 东西还是很陌生。

I'm trying to write 2 extension methods to handle Enum types. One to use the description attribute to give some better explanation to the enum options and a second method to list the enum options and their description to use in a selectlist or some kind of collection.

You can read my code up to now here:

    <Extension()> _
    Public Function ToDescriptionString(ByVal en As System.Enum) As String

        Dim type As Type = en.GetType
        Dim entries() As String = en.ToString().Split(","c)
        Dim description(entries.Length) As String

        For i = 0 To entries.Length - 1
            Dim fieldInfo = type.GetField(entries(i).Trim())
            Dim attributes() = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())

            description(i) = If(attributes.Length > 0, attributes(0).Description, entries(i).Trim())
        Next

        Return String.Join(", ", description)

    End Function

    <Extension()> _
    Public Function ToListFirstTry(ByVal en As System.Enum) As IEnumerable

        Dim type As Type = en.GetType

        Dim items = From item In System.Enum.GetValues(type) _
           Select New With {.Value = item, .Text = item.ToDescriptionString}

        Return items

    End Function

    <Extension()> _
    Public Function ToListSecondTry(ByVal en As System.Enum) As IEnumerable

        Dim list As New Dictionary(Of Integer, String)
        Dim enumValues As Array = System.Enum.GetValues(en.GetType)

        For Each value In enumValues
            list.Add(value, value.ToDescriptionString)
        Next

        Return list

    End Function

So my problem is both extension methods don't work that well together. The methods that converts the enum options to an ienumerable can't use the extension method to get the description.

I found all kind of examples to do one of both but never in combination with each other. What am I doing wrong? I still new to these new .NET 3.5 stuff.

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

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

发布评论

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

评论(1

仅冇旳回忆 2024-08-11 03:54:17

问题在于 Enum.GetValues 仅返回弱类型 Array

试试这个:(

Public Function ToListFirstTry(ByVal en As System.Enum) As IEnumerable

    Dim type As Type = en.GetType

    Dim items = From item In System.Enum.GetValues(type).Cast(Of Enum)() _
       Select New With {.Value = item, .Text = item.ToDescriptionString}

    Return items

End Function

看起来 VB 查询中显式键入的范围变量与 C# 中的含义不同。)

The problem is that Enum.GetValues just returns a weakly typed Array.

Try this:

Public Function ToListFirstTry(ByVal en As System.Enum) As IEnumerable

    Dim type As Type = en.GetType

    Dim items = From item In System.Enum.GetValues(type).Cast(Of Enum)() _
       Select New With {.Value = item, .Text = item.ToDescriptionString}

    Return items

End Function

(It looks like explicitly typed range variables in VB queries don't mean the same thing as in C#.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文