Given the two classes below, how can I
convertList<Person>to
List<Person2>?
EDIT: This is a fabricated example. The point I’m trying to make is that I can’t modify the Person class, and we’ll know arbitraryNumber when we instantiate List<Person2>.
EDIT2: I know that many of you will find this unbelievable, but this code will be deployed to a Windows-Server-2000 machine, which means that it must work with .NET 2.0. [Yes – I find this to be unbelievable as well.]
public class Person
{
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
public class Person2 : Person
{
private readonly int _arbitraryNumber;
public Person2(int arbitraryNumber)
{
_arbitraryNumber = arbitraryNumber;
}
public string FullName
{
get
{
return String.Format("{0}, {1} - {2}", LastName, FirstName, _arbitraryNumber);
}
}
}
I think Person2 needs to change to be an adapter. Something like this
Then the adapter on the collection is easier.
Or for .NET 2