I am having trouble with what should be a simple cast of a multidimensional object array (object[,]) to a new data type (string[,]) for logging purposes. The format is a dynamic two dimensional array that has many columns and rows, but doesn't fit nicely into one of the generic collection objects offered in the framework. I would just strong type it as a string[,] all the way through the process, but I need the flexibility of the object array, because in some instances I will need to use different data types.
private List<KeyValuePair<string, object>> _dataList = new List<KeyValuePair<string, object>>();
private object[,] _dataArray;
public List<KeyValuePair<string, object>> RetrieveHistoricalData()
{
...
//Calling Method (for explaination and context purposes)
_log.Log ("\r\nRetrieveHistoricalData", "_dataList.Count: " + _dataList.Count);
_dataList.ForEach(dli => _log.Log ("\r\nRetrieveHistoricalData", "_dataList: "
+ dli.Key + ((object[,])dli.Value)
.CastTwoDimensionalArray<string>()
.TwoDimensionalArrayToString()));
...
}
... Added An Extension method based on Jon Skeet's suggestions ...
internal static T[,] CastTwoDimensionalArray<T>(this object[,] dataArray)
{
int rows = dataArray.GetLength(0);
int columns = dataArray.GetLength(1);
T[,] returnDataArray = new T[rows, columns];
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
returnDataArray[row, column] =
(T)Convert.ChangeType(dataArray[row, column], typeof(T));
}
}
return returnDataArray;
}
... Here's my own addition (only included because it is in the line I am executing) ...
internal static string TwoDimensionalArrayToString<T>(this T[,] dataArray)
{
int rows = dataArray.GetLength(0);
int columns = dataArray.GetLength(1);
string returnString = "";
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
returnString = returnString + "[" + row + "," + column + "] =>" + dataArray[row,column]+ " ; ";
}
}
return returnString;
}
I have edited the code above from the first post, but I am still receiving a System.InvalidCastException when trying to convert a System.Double to a System.String in the Generic extension method. I am working on a simple way to add some exceptions through type reflection to remove the remaining issue.
Thanks.