积极答复者
vb.net 能重写enum的tostring方法么?

问题
答案
-
第三种方法:
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()
全部回复
-
只能用方法,因为它是防止覆写,但是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月29日 12:16
-
第二种写法比较简单:
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月29日 12:36
-
第三种方法:
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()