public void Test() { IBusinessObjectCollection collection = new BusinessObjectCollection(); collection.Add(new Student(2, "B")); collection.Add(new Student(1, "C")); collection.Add(new Student(3, "A")); // 验证算法的有效性 collection.Comparer = new ComparerByID(); int id = 0; foreach (IBusinessObject obj in collection.GetAll()) { Assert.AreEqual <int>(++id, obj.ID); } // 替换成另一个算法 collection.Comparer = new ComparerByName(); string[] data = new string[3] { "A", "B", "C" }; int index = 0; foreach (IBusinessObject obj in collection.GetAll()) { Assert.AreEqual <string>(data[index++], obj.Name); } }