C# Type conversion between two similar Datatable objects
Posted
by
Ali
on Stack Overflow
See other posts from Stack Overflow
or by Ali
Published on 2012-09-18T09:36:15Z
Indexed on
2012/09/18
9:37 UTC
Read the original article
Hit count: 223
I have .NET project with sync framework and two separate Datasets for MS SQL and Compact SQL. in my base class I have a generic DataTable object. in my derived classed I assign Typed DataTable to the generic object based on whether the application is operating online or offline: example:
if (online)
_dataTable = new MSSQLDataSet.Customer;
else
_dataTable = new CompactSQLDataSet.Customer;
Now every where in my code i have to check and do a cast based on the current network mode like this:
public void changeCustomerID(int ID)
{
if (online)
(MSSQLDataSet.CustomerDataTable)_dataTable)[i].CustomerID = value;
else
(CompactMSSQLDataSet.CustomerDataTable)_dataTable)[i].CustomerID = value;
}
but I don't think this is very efficient and I believe it can be done in a smarter way to only use one line of code by dynamically getting the Type of _dataTable on the run time.
my problem is at the design time, in order to acess datatable porperties such as "CustomerID" it has to be casted to either MSSQLDataSet.CustomerDataTable or CompactMSSQLDataSet.CustomerDataTable.
Is there a way to have a function or a operator to convert the _datatable to its runtime type but still be able to use it's design time properties which are the same between the two types? something like:
((aType)_dataTable)[i].CustomerID = value;
//or
GetRuntimeType(_dataTable)[i].CustomerID = value;
© Stack Overflow or respective owner