How to initialize child class using function that returns a parent class?

petunia

New Member
I've extended the \[code\]DataTable\[/code\] class as follows:\[code\]class DataTableExtended:DataTable { public specialMethod() {} }\[/code\]I want to parse an XML node into this child class, which I tried to do with the following:\[code\]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; }\[/code\]It's throwing an \[code\]InvalidCastException\[/code\]. 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 \[code\]DataTableExtended\[/code\] constructor so that it accepts a \[code\]DataTable\[/code\] argument and copies that table's information, but I was hoping that there's a more direct way of doing this.
 
Back
Top