Get Value by Property Name

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 27-Jul-2008 05:25:44   

llblgen v2.5,adapter mode,dotnet2

I want o be able to get the value for any property in a class.

eg I a have a class called carEntity with properties, color,ownername,enginesize etc

I want to create a function GetValue(Entity,PropertyName) that returns a string.

Is must be possible..(Grids etc automatically know how to label columns when you assign an entitycollection as it datasource!)..

regards Anthony (12QWERNB)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Jul-2008 20:09:10   

Anthony wrote:

I want o be able to get the value for any property in a class.

eg I a have a class called carEntity with properties, color,ownername,enginesize etc

I want to create a function GetValue(Entity,PropertyName) that returns a string.

This should work:

using System.ComponentModel;
...

public object GetValue(IEntity2 entity, string propertyName)
{
    // get the property descriptor
    PropertyDescriptorCollection realPropertyDescriptors = TypeDescriptor.GetProperties(entity.GetType());
    PropertyDescriptor property = realPropertyDescriptors[propertyName];

    // get the value
    return property.GetValue(entity);
}

Anthony wrote:

Is must be possible..(Grids etc automatically know how to label columns when you assign an entitycollection as it datasource!)..

You must define what "know how to label columns" means. By default the PropertyName is used, though I don't know if you need to do a special naming ("Customer Name" instead of "CustomerName" for instance). You could write "Custom properties" for fields at LLBLGen Pro Designer that represent the LabelText you want to use, then use those custom properties at code to label your columns (you need to write a routine that do this as it wouldn't be automatic).

David Elizondo | LLBLGen Support Team