繁体   English   中英

多维数组转换运行时错误--->无法将类型为'System.Object [,]'的对象转换为'System.String类型

[英]Multidimensional Array Casting Runtime Error ---> unable to cast object of type 'System.Object[,]' to type 'System.String

我遇到的问题是多维对象数组( 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转换为Generic扩展方法中的System.String时,我仍然收到System.InvalidCastException。 我正在研究一种通过类型反射添加一些异常以消除剩余问题的简单方法。

谢谢。

编辑:您只能从投object[,]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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM