This question already has an answer here:
- Sum of items in a collection 1 answer
I have a tree constructed using a TreeNode class where each node has an Amounts object. The Amounts object is a simple class that houses some decimal values. E.g.
public class Amounts
{
public decimal Amount1 { get; set; }
public decimal Amount2 { get; set; }
...
}
I need to be able to performsum up the individual properties independently, and what I'd like to do is somehow pass one of the Amounts properties to this method using a lambda or similar but I can't quite work out how to do this (I've been a while off the tools unfortunately). I don't particularly like the idea of passing a property name string so I can use reflection SetPropertyValue method.
The traversal method looks something like this (a little contrived of course):
public void Traverse()
{
foreach (var child in this.Children)
{
this.Amounts.Amount1 += child.Amounts.Amount1;
}
}
Rather than having a separate method for each amount, I'd like to do be able to call it something like below:
Traverse(a => a.Amount1);
I'm not sure if I have explained the requirement very well, but hopefully enough to get a pointer in the right direction.
Thanks, John
Aucun commentaire:
Enregistrer un commentaire