I've extended the DataTable
class as follows:
class DataTableExtended:DataTable
{
public void specialMethod()
{}
}
I want to parse an XML node into this child class, which I tried to do with the following:
public DataTableExtended parseNodeToDataTable()
{
DataSet ds = new DataSet();
XmlNodeReader reader = new XmlNodeReader(this.resultNodes);
ds.ReadXml(reader);
DataTable dt = ds.Tables[1];
DataTableExtended dte=(DataTableExtended) dt;
return dt;
}
It's throwing an InvalidCastException
. From what I've read so far, this is because it's not possible to cast a parent class to a child class. Is that accurate? If so, I know I can rewrite the DataTableExtended
constructor so that it accepts a DataTable
argument and copies that table's information, but I was hoping that there's a more direct way of doing this.