Hi,
I'm implementing "type-independent" method for filling DataRow with values. Intended functionality is quite straight forward - caller passes the collection of string representations of column values and DataRow that needs to be filled:
private void FillDataRow(List<ColumnTypeStringRep> rowInput, DataRow row)
ColumnTypeStringRep structure contains the string representation of value, column name, and - what's of most importance - column data type:
private struct ColumnTypeStringRep
{
public string columnName; public Type type; public string stringRep;
public ColumnTypeStrinRep(string columnName, Type type, string stringRep)
{
this.columnName = columnName; this.type = type; this.stringRep = stringRep;
}
}
So what's that "type-independency"? Basically I don't care about the data row schema (which always be a row of some typed data table), as long as passed column names match DataRow's colum names and column data types match those of DataRow I'm fine. This function needs to be as flexible as possible (and as simple as possible - just not simpler). Here it is (almost):
private void FillDataRow(List<ColumnTypeStrinRep> rowInput, DataRow row)
{
Debug.Assert(rowInput.Count == row.Table.Columns.Count);
foreach (ColumnTypeStrinRep columnInput in rowInput)
{
Debug.Assert(row.Table.Columns.Contains(columnInput.columnName));
Debug.Assert(row.Table.Columns[columnInput.columnName].DataType == columnInput.type);
// --> Now I want something more or less like below (of course the syntax is wrong) :
/*
row[columnInput.columnName] = columnInput.type.Parse(columnInput.stringRep);
*/
// --> definitely not like below (this of course would work) :
/*
switch (columnInput.type.ToString())
{
case "System.String":
row[columnInput.columnName] = columnInput.stringRep;
break;
case "System.Single":
row[columnInput.columnName] = System.Single.Parse(columnInput.stringRep);
break;
case "System.DateTime":
row[columnInput.columnName] = System.DateTime.Parse(columnInput.stringRep);
break;
//...
default:
break;
}
*/
}
}
Now You probably see my problem - I don't want to use the switch statement. It would be perfect, as in the first commented segment, to somehow use the provided column type to invoke Parse method of particular type that returns the object of that type constructed from string representation. The switch solutions works but it's extremely non flexible - what if in future I'll be filling not the DataRow but some other custom type with "columns" that can be of custom type (of course every such type will need to expose Parse method to build itself from string representation).
Hope you got what I mean - its like "dynamic parsing" kind of functionality. Is there a way to achieve this in .NET?
Example of FillDataRow call could look like this:
List<ColumnTypeStrinRep> rowInput = new List<ColumnTypeStrinRep>();
rowInput.Add(new ColumnTypeStringRep("colName1", Type.GetType("System.Int32"), "0"));
rowInput.Add(new ColumnTypeStringRep("colName2", Type.GetType("System.Double"), "1,11"));
rowInput.Add(new ColumnTypeStringRep("colName3", Type.GetType("System.Decimal"), "2,22"));
rowInput.Add(new ColumnTypeStringRep("colName4", Type.GetType("System.String"), "test"));
rowInput.Add(new ColumnTypeStringRep("colName5", Type.GetType("System.DateTime"), "2010-01-01"));
rowInput.Add(new ColumnTypeStringRep("colName6", Type.GetType("System.Single"), "3,33"));
TestDataSet.TestTableRow newRow = this.testDataSet.TestTable.NewTestTableRow();
FillDataRow(rowInput, newRow);
this.testDataSet.TestTable.AddTestTableRow(newRow);
this.testDataSet.TestTable.AcceptChanges();
Thank You!