0

我在将多维对象数组 ( object[,]) 简单转换为新数据类型 ( string[,]) 以进行日志记录时遇到了麻烦。该格式是一个动态二维数组,有许多列和行,但不能很好地适应框架中提供的通用集合对象之一。我会在整个过程中将它作为一个强类型string[,],但我需要对象数组的灵活性,因为在某些情况下我需要使用不同的数据类型。

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()));

...

}

... 添加了基于 Jon Skeet 建议的扩展方法 ...

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;
}

...这是我自己的补充(仅包括在内,因为它在我正在执行的行中)...

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;
}

我已经从第一篇文章中编辑了上面的代码,但是在尝试在通用扩展方法中将 System.Double 转换为 System.String 时,我仍然收到 System.InvalidCastException。我正在研究一种通过类型反射添加一些异常以消除剩余问题的简单方法。

谢谢。

4

1 回答 1

2

编辑:如果所涉及的数组确实是原始数组,则只能从object[,]to 转换。例如,这很好:string[,]string[,]

object[,] o = new string[,]
{
    { "x", "y" },
    { "a", "b" }
};
string[,] x = (string[,]) o;

...但这不是:

object[,] o = new object[,]
{
    { "x", "y" },
    { "a", "b" }
};
string[,] x = (string[,]) o; // Bang!

即使数组的每个元素都是一个字符串,它仍然不是一个字符串数组。在后一种情况下,您需要编写自己的代码来创建新的数组对象并执行元素转换。例如,这是一个可用于转换每个元素的通用方法:

using System;

class Test
{
    static void Main()
    {
        object[,] o = new object[,]
        {
            { "x", "y" },
            { "a", "b" }
        };
        string[,] x = Cast2D<string>(o);
        Console.WriteLine(x[1, 1]); // "b"
    }

    static T[,] Cast2D<T>(object[,] input)
    {
        int rows = input.GetLength(0);
        int columns = input.GetLength(1);
        T[,] ret = new T[rows, columns];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                ret[i, j] = (T) input[i, j];
            }
        }
        return ret;
    }        
}

在您的情况下,您可能想要:

object[,] array = (object[,]) financialDataObject;
string[,] financialData = Cast2D<string>(array);
于 2013-01-02T17:07:43.900 回答