Re: Custom Attributes, Shared methods, Derived classes and reflection



On 31 Jul 2006 12:52:37 -0700, tommaso.gastaldi@xxxxxxxxxxx wrote:

Yes Jeff, I know it's a little bit hackery :) But it gets the job done
in a way that is, somehow, general. If I find a more elegant way I will
tell you, but I doubt it can be done with "pure" reflection, which is
based on instances. Actually, I was trying to be of help in an attempt
to convince myself that attributes are really useful for something.
But, at the present, I can see ways to do the same things in a much
more clear and maintenable way and I am not really persuaded of the
real usefulness of this stuff. For instance, often, enumerations used
in conjuction with hashtables and classes of attributes can get the job
done in a much more clear and maintenable way. I hope in the future to
see some useful way to integrate this notion in my programs, but so
far, frankly speaking, I am unable to give to this attribute stuff an
overwhelming importance. If you see situations where attributes are
really a useful replacement of other technique I would be very
interested to know them :)

-Tom

Tom,

We found the following class (watch wrapping) which uses attributes useful. We use
enumerations all over the place, and it is helpful sometimes to define a "friendly"
description associated with an enum value. Of course, the ToString method on an
enumeration value will give you the name as defined by the enumeration, but we've
found it helpful to be able to attach an alternate Friendly Name to some enumerations
(to load up a combo box, say).

This uses the DescriptionAttribute located in the System.ComponentModel namespace
(since that was handy), but the technique could be applied to a custom attribute if
you're so inclined.

Thanks for your help, and it looks like I'll be abandoning the idea of a custom
attribute because unfortunately my requirement is that instances may not exist.

-----
Imports System.Reflection
Imports System.ComponentModel

Public Class EnumHelper

'Given an enumeration value, return its description attribute of that value.
'e.g. given:
'
' Public Enum people
' <Description("Jeff Mason")> Jeff
' <Description("Dirk Digler")> Dirk
' <Description("John Smith")> John
' <Description("Sam Spade")> Sam
' End Enum
'
'then calling this:
'
' GetEnumDescription(people.Jeff)
'
'would return this:
'
' "Jeff Mason"
'
Public Shared Function GetEnumDescription(ByVal value As [Enum]) As String

Dim fi As FieldInfo = value.GetType().GetField(value.ToString)
Dim attribs() As DescriptionAttribute =
DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False),
DescriptionAttribute())

If attribs.Length > 0 Then
Return attribs(0).Description
Else
Return value.ToString
End If

End Function

'Given an enumeration and the description of one of the items in it,
'get the value of the corresponding enumeration item.
'
'Given the people enum as above, calling:
'
' GetEnumValue(GetType(people), "John Smith")
'
'would return:
'
' 2
'
'Note that this function is optimized for integer enums. Use GetEnumValueObj if
the
'enum is some other type.
Public Shared Function GetEnumValue(ByVal t As Type, ByVal description As String)
As Integer

For Each e As [Enum] In e.GetValues(t)
If GetEnumDescription(e) = description Then
'its this one, so get the value from it
Dim i As FieldInfo = e.GetType().GetField(e.ToString)
Return CInt(i.GetValue(e))
End If
Next

Return Nothing

End Function

Public Shared Function GetEnumValueObj(ByVal t As Type, ByVal description As
String) As Object

For Each e As [Enum] In e.GetValues(t)
If GetEnumDescription(e) = description Then
'its this one, so get the value from it
Dim i As FieldInfo = e.GetType().GetField(e.ToString)
Return i.GetValue(e)
End If
Next

Return Nothing

End Function

'Given an enumeration and a description, check if any items in the enumeration
'have that description.
'
'Given the people enum as above, calling:
'
' GetEnumValueExists(GetType(people), "John Smith")
'
'would return true
'
'On the other hand calling:
'
' GetEnumValueExists(GetType(people), "Bob Jones")
'
'would return false
'
Public Shared Function GetEnumValueExists(ByVal t As Type, ByVal description As
String) As Boolean

For Each e As [Enum] In e.GetValues(t)
If GetEnumDescription(e) = description Then
'there is a match
Return True
End If
Next
'there isn't
Return False

End Function

'Get an arraylist of descriptions from an enumeration.
'(useful for populating comboboxes, etc.)
Public Shared Function GetEnumDescriptionArrayList(ByVal t As Type) As ArrayList

Dim al As New ArrayList
For Each e As [Enum] In e.GetValues(t)
al.Add(GetEnumDescription(e))
Next

Return al

End Function

End Class

-- Jeff
.



Relevant Pages

  • Re: Custom Attributes, Shared methods, Derived classes and reflection
    ... description associated with an enum value. ... enumeration value will give you the name as defined by the enumeration, ... Dim fi As FieldInfo = value.GetType.GetField ... Public Shared Function GetEnumValue ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Enumeration and Description
    ... Public Shared Function GetDescription(ByVal value As [Enum]) As String ... Dim fi As FieldInfo = value.GetType.GetField) ... DescriptionAttribute()) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Enumerations And Random Numbers
    ... > Fiend Enum TravelDirection ... > number and assign it, however, As I add to the enumeration, I wont want to ... Dim r As New Random ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Variable arguments of enum type
    ... the implementation type used for the enumeration type as a whole. ... I used (unsigned int) instead of against the off chance that the ... enum EnumType1 { ... enum EnumType2 can be distinct from enum EnumType1; ...
    (comp.lang.c)
  • Re: PEP 354: Enumerations in Python
    ... -1 for this particular proposal as a builtin. ... design of something that isn't terribly useful to begin with. ... As for preventing nonsensical operations on enum constants: ... Some alternative designs implement each enumeration as its own ...
    (comp.lang.python)