多维数组强制转换运行时错误——>无法强制类型为'System.Object[,]'键入'Syst

本文关键字:Object System Syst 键入 类型 转换 数组 运行时错误 | 更新日期: 2023-09-27 17:50:20

我遇到了麻烦,应该是一个简单的多维对象数组(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;
}

我已经编辑了上面第一篇文章的代码,但我仍然收到一个系统。当尝试转换系统时,InvalidCastException。Double to a System。泛型扩展方法中的字符串。我正在研究一种简单的方法,通过类型反射添加一些异常,以消除剩余的问题。

谢谢。

多维数组强制转换运行时错误——>无法强制类型为'System.Object[,]'键入'Syst

编辑:如果所涉及的数组最初确实是 string[,],则只能从object[,]强制转换为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);