如何在C#中将2D对象数组转换为2D字符串数组?

时间:2016-05-27 04:35:57

标签: c# string object multidimensional-array

我有一个名为'value'的2D对象数组。我想将其转换为2D字符串数组。我该怎么做?

Employee(x, None, "Singri")

如果可能的话,我也想知道我是否可以将对象转换为

object value; //This is a 2D array of objects
string prop[,]; //This is a 2D string

直接

1 个答案:

答案 0 :(得分:1)

这是你在找什么?

        string[,] prop; //This is a 2D string
        List<List<string>> mysteryList;

        if (value is object[,])
        {
            object[,] objArray = (object[,])value;

            // Get upper bounds for the array
            int bound0 = objArray.GetUpperBound(0);//index of last element for the given dimension
            int bound1 = objArray.GetUpperBound(1);

            prop = new string[bound0 + 1, bound1 + 1];
            mysteryList = new List<List<string>>();

            for (int i = 0; i <= bound0; i++)
            {
                var temp = new List<string>();

                for (int j = 0; j <= bound1; j++)
                {
                    prop[i, j] = objArray[i, j].ToString();//Do null check and assign 
                    temp.Add(prop[i, j]);
                }
                mysteryList.Add(temp);
            }
        }