【发布时间】:2011-11-15 15:08:43
【问题描述】:
public abstract class ContentManagedEntity
{
public Guid Guid { get; set; }
public bool Active;
public int DisplayOrder;
}
public class StoreCategory : ContentManagedEntity
{
public string Name { get; set; }
}
public class XMLStoreCategory : StoreCategory, IXMLDataEntity
{
public bool Dirty = false;
}
void main() {
var storecategory = new StoreCategory { Name = "Discount Stores" };
var xmlstorecategory = (XMLStoreCategory) storecategory; // Throws InvalidCastException
}
它在运行时最后一行抛出 InvalidCastException 是否有原因?
(呸,当我写这篇文章时,答案突然出现在我的脑海中,一清二楚。张贴给后代,只是为了确保我做对了。)
【问题讨论】:
标签: c# .net inheritance casting