我认为Person2需要改变成一个适配器。像这样的东西
public class Person2
{
private readonly int _arbitraryNumber;
private readonly Person _person;
public Person2(Person person, int arbitraryNumber)
{
_arbitraryNumber = arbitraryNumber;
_person = person;
}
public string FullName
{
get
{
return String.Format("{0}, {1} - {2}", _person.LastName, _person.FirstName, _arbitraryNumber);
}
}
}
那么集合上的适配器就更容易了。
List<Person> persons = new List<Person>();
// put some people in it.
var person2s = persons.Select(p => new Person2(4, p)); // 4 chosen arbitrarily
或.NET 2
List<Person2> person2s = new List<Person2>();
foreach (Person p in persons)
{
Person2 person2 = new Person2(p, 11); // 11 arbitrarily chosen by asking a kid for a number
person2s.Add(person2);
}