捕获列表
本文关键字:事件 object 列表 | 更新日期: 2023-09-27 18:02:17
让我尽可能简单地解释我的问题。假设我有一个类Product
class Product {
public event EventHandler Product_Changed;
int _productId ;
int productID {
get { return _productId}
set{
_productId = value;
Product_Changed(this,null); // Raise an event that the product Changed
}}
string productName {get;set;}
}
然后在另一个类中,我需要使用产品列表,每当任何产品的ProductId更改时,我需要捕获在产品类
中定义的事件Class Order
{
List<Product> OrderProduct = new List<Product>();
OrderProduct.Add (new Product());
// Then I change the productId like
OrderProduct[0].ProductID=10 ; // I want to catch the Product_Changed
}
请帮忙
你可以这样做:
class Order
{
List<Product> OrderProduct = new List<Product>();
var newProduct = new Product();
newProduct.Product_Changed += (sender, e) => {
// do something - sender contains the current product
}
OrderProduct.Add(newProduct);
// Then I change the productId like
OrderProduct[0].ProductID=10 ; // I want to catch the Product_Changed
}