none
vb.net 能重写enum的tostring方法么? RRS feed

  • 问题

  • 要vb.net ,,c#看不懂

    我想枚举的是

     Public Enum TTT
                   D1.5 =0

                   D2.5 =0

     End Enum

    但是枚举里不能写成带小数点的,所以只能

    Public Enum TTT
                   D15 =0

                   D25=1

     End Enum

    现在想tostring时能不能输出"D1.5","D2.5"


    Work8862

    2017年8月27日 16:24

答案

  • 第三种方法:

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Module Module1
        <Extension()> _
        Public Function ToString2(ByVal EnumConstant As [Enum]) As String
            Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
            Dim aattr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
            If aattr.Length > 0 Then
                Return aattr(0).Description
            Else
                Return EnumConstant.ToString()
            End If
        End Function
    
    End Module
    Imports System.ComponentModel
    
    Public Enum MyEnums
        <Description("D1.5")> D15 = 1
        <Description("D2.5")> D25 = 2
    End Enum

    TextBox1.Text = MyEnums.D15.ToString2()



    2017年8月27日 18:03

全部回复

  • 只能用方法,因为它是防止覆写,但是Enum的概念我们可以用程式码实现,所以下面三个是基本例子(汞是辛苦的程式吗,如果是您需要的请设置作为答复,谢谢您):

    NotInheritable Class Enum1
    	Private Sub New()
    	End Sub
    	'[Extension]
    	<System.Runtime.CompilerServices.Extension> _
    	Public Shared Function GetDescription(Of T As Structure)(enumerationValue As T) As String
    		Dim type As Type = enumerationValue.[GetType]()
    		If Not type.IsEnum Then
    			Throw New ArgumentException("EnumerationValue must be of Enum type", "enumerationValue")
    		End If
    
    		'Tries to find a DescriptionAttribute for a potential friendly name
    		'for the enum
    		Dim memberInfo As MemberInfo() = type.GetMember(enumerationValue.ToString())
    		If memberInfo IsNot Nothing AndAlso memberInfo.Length > 0 Then
    			Dim attrs As Object() = memberInfo(0).GetCustomAttributes(GetType(DescriptionAttribute), False)
    
    			If attrs IsNot Nothing AndAlso attrs.Length > 0 Then
    				'Pull out the description value
    				Return DirectCast(attrs(0), DescriptionAttribute).Description
    			End If
    		End If
    		'If we have no description attribute, just return the ToString of the enum
    		Return enumerationValue.ToString()
    	End Function
    
    	Private Enum PublishStatusValue
    		<Description("D1.5")> _
    		D15 = 1
    		<Description("D2.5")> _
    		D25 = 2
    	End Enum
    End Class


















    2017年8月27日 17:34
  • 第二种写法比较简单:

    Public NotInheritable Class Enum2
    
        Private ReadOnly name As String
        Private ReadOnly value As Integer
    
        Public Shared ReadOnly D15 As New Enum2(0, "D1.5")
        Public Shared ReadOnly D25 As New Enum2(1, "D2.5")
    
        Private Sub New(ByVal value As Integer, ByVal name As String)
            Me.name = name
            Me.value = value
        End Sub
    
        Public Overrides Function ToString() As String
            Return name
        End Function
    End Class






    2017年8月27日 17:55
  • 第三种方法:

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Module Module1
        <Extension()> _
        Public Function ToString2(ByVal EnumConstant As [Enum]) As String
            Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
            Dim aattr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
            If aattr.Length > 0 Then
                Return aattr(0).Description
            Else
                Return EnumConstant.ToString()
            End If
        End Function
    
    End Module
    Imports System.ComponentModel
    
    Public Enum MyEnums
        <Description("D1.5")> D15 = 1
        <Description("D2.5")> D25 = 2
    End Enum

    TextBox1.Text = MyEnums.D15.ToString2()



    2017年8月27日 18:03