开发者

Run code after/before adding item to an Collection in .NET

开发者 https://www.devze.com 2023-03-08 20:45 出处:网络
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

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."

0

精彩评论

暂无评论...
验证码 换一张
取 消