|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Build reflection call into base class?I'm looking for a way to expose property attributes in a derived class. I'm
declaring Description attributes like this: Public Class Employee <Description("Last Name")> _ Public Overridable Property NameLast() As String .... I'd like to somehow be able to refer to the description with syntax like this: objEmployee.NameLast.Description. I'm new to reflection, but I've worked out how to get at the attributes collection and get to this particular attribute, but I'd like to have the base class contain this functionality so that it is available to every property in every derived class without having to code it for each class and property. Any suggestions would be very welcome. Barry 2 Points:
1) Reflection is slow and enumerating Custom Attributes is one of the slowest things you can do. 2)There is no way to retrieve your syntax goal so you would need to implement a function like GetDescription("PropertyName"), if you are going to do this you may as well cut out reflection and have them as properties on the object back by resources or have them as public readonly fields. Show quote "Barry Gilbert" wrote: > I'm looking for a way to expose property attributes in a derived class. I'm > declaring Description attributes like this: > > Public Class Employee > <Description("Last Name")> _ > Public Overridable Property NameLast() As String > ... > > I'd like to somehow be able to refer to the description with syntax like this: > objEmployee.NameLast.Description. I'm new to reflection, but I've worked out > how to get at the attributes collection and get to this particular attribute, > but I'd like to have the base class contain this functionality so that it is > available to every property in every derived class without having to code it > for each class and property. > > Any suggestions would be very welcome. > > Barry Ciaran
Thanks for your reply. I already have a function like you describe. I'm not concerned about speed; this will be called once for each column in a DatagridView to set the HeaderText properties. It's only called when the form is loaded. I'm conflicted on whether to use property attributes, a db table with property names and descriptions, or resources/settings. I lean toward property attributes, but no solution seems perfect. I suspected this wouldn't be possible. I guess I'll have to rewrite the .Net Framework. :-) Thanks, Barry Show quote "Ciaran O''''Donnell" wrote: > 2 Points: > 1) Reflection is slow and enumerating Custom Attributes is one of the > slowest things you can do. > 2)There is no way to retrieve your syntax goal so you would need to > implement a function like GetDescription("PropertyName"), if you are going to > do this you may as well cut out reflection and have them as properties on the > object back by resources or have them as public readonly fields. > > > -- > Ciaran O''''Donnell > http://wannabedeveloper.spaces.live.com
Show quote
"Barry Gilbert" <BarryGilb***@discussions.microsoft.com> wrote in message Sounds like you want something similar to news:7E685145-4DE7-4B38-A183-B7AEBCAA02C2@microsoft.com... > Ciaran > Thanks for your reply. I already have a function like you describe. I'm > not > concerned about speed; this will be called once for each column in a > DatagridView to set the HeaderText properties. It's only called when the > form > is loaded. > > I'm conflicted on whether to use property attributes, a db table with > property names and descriptions, or resources/settings. I lean toward > property attributes, but no solution seems perfect. > > I suspected this wouldn't be possible. I guess I'll have to rewrite the > .Net > Framework. :-) System.ComponentModel.PropertyDescriptor, and especially System.ComponentModel.TypeDescriptor.GetProperties (Type) Show quote > > Thanks, > Barry > "Ciaran O''''Donnell" wrote: > >> 2 Points: >> 1) Reflection is slow and enumerating Custom Attributes is one of the >> slowest things you can do. >> 2)There is no way to retrieve your syntax goal so you would need to >> implement a function like GetDescription("PropertyName"), if you are >> going to >> do this you may as well cut out reflection and have them as properties on >> the >> object back by resources or have them as public readonly fields. >> >> >> -- >> Ciaran O''''Donnell >> http://wannabedeveloper.spaces.live.com > Ben,
That's what I used. My routine looks like this: Public Shared Function PropertyDescription(ByVal obj As Object, ByVal propName As String) As String Dim attributes As AttributeCollection = _ TypeDescriptor.GetProperties(obj)(propName).Attributes Dim attr As DescriptionAttribute = _ CType(attributes(GetType(DescriptionAttribute)), DescriptionAttribute) If attr.Description = "" Then Return propName Else Return attr.Description End If End Function Thanks, Barry Show quote "Ben Voigt" wrote: > > Sounds like you want something similar to > System.ComponentModel.PropertyDescriptor, and especially > System.ComponentModel.TypeDescriptor.GetProperties (Type) > |
|||||||||||||||||||||||