I'm pretty sure this is impossible, but here goes..
I have a custom class in C# called Person which has a few properties such as Age, Height etc.
I then make a new class called Employee which inherits from Person but I don't yet add any other properties to Employee. So its basically just a Person still, except its called an Employee.
Now say I have an instance of Person called SomePerson. How can I create a new Employee instance that has all of the values it inherited from Person, set to those inside SomePerson. Like casting from a Person into an Employee.. But without me having to manually specify each and every property that needs to be set..
Something like..
Employee NewEmployee = (Employee)SomePerson;
But of course you get the error saying "Can't convert a Person into an Employee" etc.
Is AutoMapper the only practical solution to do things like this if you say had 300 properties in the involved objects??
UPDATE:
Auto-Mapper doesn't seem to handle my objects..
Employee SomeEmployee = EmployeeRepository.GetEmployee(SomeEmployeeID);
// Populate the ViewModel with the Person fetched from the db, ready for editing..
VMEmployee EmployeeToEdit = Mapper.Map<Employee, VMEmployee>(SomeEmployee);
// ViewModel based on Employee with Validation applied..
[MetadataType(typeof(Employee_Validation))]
public class VMEmployee : Employee
{
// Absolutely nothing here
}
where "Employee" is auto-generated by LINQ to SQL..
VMEmployee : Employee. You need to define your view model with a subset of properties from yourEmployeedomain model.