Finding properties by Name

Finding properties by Name

Old forum URL: forums.lhotka.net/forums/t/6346.aspx


nj609eagle posted on Thursday, February 05, 2009

I have a long list of properties in my Object.  These properties coorespond to a Line Items on an Application, sorta like your tax return.  Each line has a Chargeable, Non-Chargeable and Total property.

I would like to use the property changed even to place a generic routine to update the total properties. All of the Properties are similearly name "line20c", "line20nc", "line20t"..and so on.

I tried:

Dim strLine As String = Left(e.PropertyName, 6)
Dim LineTotal As Decimal = 0

Dim ChargabelProperty As Csla.PropertyInfo(Of System.Decimal) = Me.FieldManager.GetRegisteredProperties(strLine & "c")


Dim NonChargabelProperty As Csla.PropertyInfo(Of System.Decimal) = Me.FieldManager.GetRegisteredProperties(strLine & "c")

Dim TotalProperty As Csla.PropertyInfo(Of System.Decimal) = Me.FieldManager.GetRegisteredProperties(strLine & "c")

LineTotal = LineTotal + GetProperty(Of System.Decimal)(ChargabelProperty)
LineTotal = LineTotal + GetProperty(Of System.Decimal)(NonChargabelProperty)

SetProperty(Of System.Decimal)(TotalProperty, LineTotal)

 

But GetRegisteredProperties expects the index number not name

rsbaker0 replied on Thursday, February 05, 2009

I have a similar requirement. I solved this by maintaining a lazy-loaded static dictionary type object in my own class for doing name-based lookup. The first time a lookup is done, the entire property info list is retreived and added by name. After that, you can just index them yourself anyway you prefer.

This is C#, but here is my code for doing this...

(OT: can someone please show me how to post code samples that look decent? )

static Dictionary<string, IPropertyInfo> _propertyTable;

internal Dictionary<string, IPropertyInfo> ManagedPropertyTable

{

get

{

if (_propertyTable == null)

{

_propertyTable = new Dictionary<string, IPropertyInfo>();

LoadManagedPropertyTable();

}

return _propertyTable;

}

}

void LoadManagedPropertyTable()

{

var properties = FieldManager.GetRegisteredProperties();

foreach (var item in properties)

{

_propertyTable.Add(item.Name, item);

}

}

SonOfPirate replied on Thursday, February 05, 2009

My suggestion would be to use LINQ on the list returned from GetRegisteredProperties().  Something like:

Dim chargableProperty = Me.FieldManager.GetRegisteredProperties().First(Function(p) p.Name = strLine & "c")

 

rsbaker0 replied on Thursday, February 05, 2009

SonOfPirate:

My suggestion would be to use LINQ on the list returned from GetRegisteredProperties().  Something like:

Dim chargableProperty = Me.FieldManager.GetRegisteredProperties().First(Function(p) p.Name = strLine & "c")

 

Is that an indexed search or is it going to scan the entire list? That would be my concern with using that approach (since the OP said there were many properties).

SonOfPirate replied on Thursday, February 05, 2009

If the original list isn't indexed, which I don't believe it would be, then no.  However, LINQ is supposed to (I said supposed to!) be more performant when executing these types of queries.  Should be simple enough to benchmark.  Let us know what you find out.

 

nj609eagle replied on Friday, February 06, 2009

SonOfPirate:

My suggestion would be to use LINQ on the list returned from GetRegisteredProperties().  Something like:

Dim chargableProperty = Me.FieldManager.GetRegisteredProperties().First(Function(p) p.Name = strLine & "c")

 

This worked very nicely and clean.  I was doing a for counter loop and doing a name compare.  but the code wasn't as clean looking as this...thats a bunch

Copyright (c) Marimer LLC