KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I get the following exception: InvalidCastException: Unable to cast object of type 'Employee' to type 'EmployeeProfile'. I have the following code: private class Employee { public string Name { get; private set; } public Employee() { this.Name = "employee"; } public override string ToString() { return this.Name; } } private class EmployeeProfile : Employee { public string Profile { get; private set; } public EmployeeProfile() : base() { this.Profile = string.Format("{0}'s profile", this.Name); } public override string ToString() { return this.Profile; } } public void RunTest() { Employee emp = new Employee(); EmployeeProfile prof = (EmployeeProfile)emp; // InvalidCastException here System.Console.WriteLine(emp); System.Console.WriteLine(prof); } Maybe my brain is burned out, but I thought you can cast a subtype to its base type? What am I missing here? Maybe it is a vacation... thank you!
Tags (comma-separated)
Save Edits
Cancel