Given the following class definitions:
public class BaseClass { public string SomeProp1 { get; set; } } public class DerivedClass : BaseClass { public string SomeProp2 { get; set; } }
How can I take a List<BaseClass> and convert it to a List<DerivedClass>?
In my real-world scenario BaseClass has a whole bunch of properties that I don’t want to have to copy over one-by-one (and then remember to maintain if an additional property gets added).
Adding a parameterised constructor to BaseClass is not an option as this class is defined by a WCF service reference.
Actually ConvertAll is good when you need to create new objects based on the original, when you just need to cast you can use the following
If not all of the items in your list can be cast to DerivedClass then use OfType instead