views:

33

answers:

1

I have an object and I iterate over object's properties. Foreach property, i append it as a column to a listview.

In my object, some properties doesn't have accents. But in portuguese, they do. For example: "Endereco" property must be "Endereço".

I need an away to create an alias to the properties. Is it possible in VB.NET? Thank you.

+2  A: 

This sounds like job for attributes. You can use an attribute to decorate your properties with the exact header information you want. For example:

<Description("Endereço")> _
Public Property String Endereco ....

And then use reflection to pull that value out in code. You can also use the attribute value as a key into a localization table, to make future translation to other languages easier as well, or use other attributes to do things like tell your view not so show a particular field in certain circumstances.


To answer your reflection question, I'm assuming you're already at least a little comfortable with it, as that's how you have to iterate over an object's properties (unless what you really have is a dictionary, in which case you should have said so). So as you're iterating over the properties the objects you're working with are of type PropertyInfo, and you can call the GetCustomAttributes() method for that type to find any attributes associated with the property.

Joel Coehoorn
Do you have the code for the Reflection? Thank you very much!
Alson
I hope this helps: http://msdn.microsoft.com/en-us/library/b05d59ty.aspx
asawyer
Oh, and this will spit out an IEnumerable list of KeyValuePair objects for a given class with property name and values: public Shared Function GetPropertyValueList(ByVal obj As Object) As IEnumerable(Of KeyValuePair(Of String, String)) Return From p In obj.GetType().GetProperties() Select New KeyValuePair(Of String, String)(p.Name, p.GetValue(obj, Nothing).ToString()) End Function
asawyer