我有一个类,例如:上溯造型相关 - 类转换
class BaseDataPoint
{
public double A;
public double B;
}
和稍后在代码,我需要延伸到
class ReportDataPoint : BaseDataPoint
{
public double C;
}
由于以上仅仅是一个例子,并有更多的领域复制,是否有可能(通过一些技巧)'转换'BaseDataPoint
实例到ReportDataPoint
而不需要手动复制逐个字段?
如果我有
BaseDataPoint p1;
...
ReportDataPoint p2=(ReportDataPoint)p1; // FAILS at runtime
ReportDataPoint p3=new ReportDataPoint(p1); // can't compile