我有两个类,名为Post和Question。Question的定义如下:
public class Question : Post
{
//...
}
我的Question类没有覆盖Post的任何成员,只是表达了一些其他成员。
我想实现的目标
我有一个类型为Post的对象,其成员已填充。现在,我想将其转换为Question,以便我可以为那几个其他成员添加值。
这是我的当前代码,使用显式转换:
Post postToQuestion = new Post();
//Populate the Post...
Question ques = (Question)postToQuestion; //--> this is the error!
//Fill the other parts of the Question.
问题
我遇到了一个 InvalidCastException 异常。我做错了什么?