日期:2014-05-18 浏览次数:21002 次
public class A { public List<int> item { get; set; } //我在A类里怎么能监视这个item的增减? }
A a = new A(); a.item.add(1); //调用的地方只执行这些代码不需要额外代码
class Program { static void Main(string[] args) { A a = new A(); a.AddEvent += new AddHandler(a_AddEvent); a.Add(123); a[1] = 1234; } static void a_AddEvent(string messsage,int value) { Console.WriteLine("{0} 添加了{1}",messsage,value.ToString()); } } public delegate void AddHandler(string message, int value); public class A { public event AddHandler AddEvent = null; private List<int> item = new List<int>(); public int this[int index] { get { return this.item[index]; } set { this.addValue("索引器",value); } } public void Add(int value) { this.addValue("A.Add",value); } private void addValue(string message,int value) { this.item.Add(value); if (this.AddEvent != null) { this.AddEvent(message,value); } } }