Run code after/before adding item to an Collection in .NET
Is it possible to run code aftter or before the item was added to collection?
Lets say i have object Product with property Price and Warehouse object with properties IList Products and TotalPrice. I need to update TotalPrice each time the collection was modified. I dont want to calculate TotalPrice inside that property each time its accessed.
If it is possible only with AOP, is 开发者_StackOverflowthere any free AOP framework for .net?
Take a look at ObservableCollection<T>
, which is a collection designed especially for the scenario you described:
MSDN link: "Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed."
精彩评论