【发布时间】:2010-12-26 02:48:43
【问题描述】:
我有两个类,名为 Post 和 Question。问题定义为:
public class Question : Post
{
//...
}
我的 Question 类没有覆盖 Post 的任何成员,它只是表达了其他一些成员。
我想要完成的事情
我有一个 Post 类型的对象,它的成员被填充。现在,我想将其转换为问题,以便为其他少数成员添加值。
这是我当前的代码,使用显式强制转换:
Post postToQuestion = new Post();
//Populate the Post...
Question ques = (Question)postToQuestion; //--> this is the error!
//Fill the other parts of the Question.
问题
我收到 InvalidCastException。我做错了什么?
【问题讨论】:
标签: c# .net oop inheritance casting