c# - Convert monetary properties of an object -
my project has following structures:
public struct money { public currencycodes currency; public decimal amount; } public class foo { public money adultfare { get; set; } public money childfare { get; set; } public money babyfare { get; set; } public money adultfee { get; set; } public money childfee { get; set; } public money babyfee { get; set; } public money totalfare { get; set; } public money totalfee { get; set; } }
now need convert foo monetary fields 1 currency another. best solution design? use reflection? idea?
i suggest making of v
fields array, so:
public class foo { public money[] v { get; set; } // instantiate "new money[10]" }
you go through v
array , convert each one, so:
// in class foo public void convertallmoney(currencycodes newcurrency) { foreach (money m in v) m = m.convertto(newcurrency); }
alternatively, if don't want make array, in fact use reflection, suggested:
// in class foo public void convertallmoney(currencycodes newcurrency) { foreach (var p in typeof(foo).getproperties().where(prop => prop.propertytype == typeof(money))) { money m = (money)p.getvalue(this, null); p.setvalue(this, m.convertto(newcurrency), null); } }
edit: want use second suggestion, reflection, variables not in form of list.
Comments
Post a Comment