我得到了以下异常:
InvalidCastException:无法将类型为“Employee”的对象强制转换为类型“EmployeeProfile”。
我有以下代码:
也许我的大脑已经烧坏了,但我认为你可以将子类型转换为其基础类型?我错过了什么吗?也许这是一个假期...谢谢!
InvalidCastException:无法将类型为“Employee”的对象强制转换为类型“EmployeeProfile”。
我有以下代码:
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);
}
也许我的大脑已经烧坏了,但我认为你可以将子类型转换为其基础类型?我错过了什么吗?也许这是一个假期...谢谢!