Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason <je.mason@xxxxxxxxxxx>
- Date: Mon, 31 Jul 2006 16:51:59 -0400
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
.
- Follow-Ups:
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- References:
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: Jeff Mason
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- From: tommaso . gastaldi
- Re: Custom Attributes, Shared methods, Derived classes and reflection
- Prev by Date: TAB key in DataGrid
- Next by Date: Re: SqlDataAdapter Questions
- Previous by thread: Re: Custom Attributes, Shared methods, Derived classes and reflection
- Next by thread: Re: Custom Attributes, Shared methods, Derived classes and reflection
- Index(es):
Relevant Pages
|