using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace ConsoleApplication31
{
class Program
{
public static Array ReturnArray()
{
string[,,] arr = new string[, , ];
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{ for (int k = ; k < ; k++)
{ arr[i, j, k] = i + "," + j + "," + k;
} } }
return arr; } private static Array GetJCSZ()
{
//string[][][] arr = new string[2][][];
//arr[0] = new string[2][];
//arr[0][0] = new string[3];//3
//arr[0][1] = new string[4];//4
//arr[0][0][0] = "0,0,0";
//arr[0][0][1] = "0,0,1";
//arr[0][0][2] = "0,0,2"; //arr[1] = new string[2][];
//arr[1][0] = new string[3];
//arr[1][1] = new string[4];
//arr[1][0][0] = "1,0,0";
//arr[1][0][1] = "1,0,1";
//arr[1][0][2] = "1,0,2"; //return arr; string[] arr = new string[];
arr[] = "";
arr[] = "";
return arr;
} public static void InitColumns(Array arr, ref Dictionary<string, DataColumn> dicCols, ref DataTable table)
{ for (int i = ; i < arr.Length; i++)
{ if ((arr as dynamic)[i] is Array)
{ InitColumns((arr as dynamic)[i], ref dicCols, ref table); }
else
{ if (arr.Length >= dicCols.Keys.Count)
{
dicCols.Clear();
for (int ii = ; ii < arr.Length; ii++)
{ string colName = Guid.NewGuid().ToString();
DataColumn col = new DataColumn(colName);
if (!dicCols.ContainsKey(colName))
{
dicCols.Add(colName, col); } } } } } } public static DataTable ArrayConvert2DataTable(Array arr)
{
DataTable tmpT = new DataTable();
Dictionary<string, DataColumn> dicCols = new Dictionary<string, DataColumn>();
Dictionary<string, DataRow> dicRows = new Dictionary<string, DataRow>();
//J=交 C=错
bool isJC = !(arr.GetType().Name.Contains(','));
//交错数组处理
if (isJC)
{
//交错数组第一个维度的元素个 DataTable table = new DataTable();
List<int> dims = new List<int>();
InitColumns(arr, ref dicCols, ref table);
foreach (var item in dicCols)
{
table.Columns.Add(item.Value);
}
int currRowIndex = ;
SearchTable(ref currRowIndex,arr,arr, ref table); return table; }
//多维数组处理
else
{ int rank = arr.Rank;
int cols = arr.GetLength(rank - ); for (int i = ; i < cols; i++)
{
DataColumn col = new DataColumn(Guid.NewGuid().ToString());
tmpT.Columns.Add(col);
} Dictionary<int, int> dims = new Dictionary<int, int>();
int currRowIndex = -;
Dictionary<int, DataRow> dicRow = new Dictionary<int, DataRow>();
var iterator = arr.GetEnumerator();
int count = ;
while (iterator.MoveNext())
{
var curr = iterator.Current;
if (count % cols == )
{
currRowIndex++;
DataRow dr = tmpT.NewRow();
tmpT.Rows.Add(dr);
dicRow.Add(currRowIndex, dr);
dr[] = curr.ToString();
if (count == cols)
{
count = ;
} }
else
{
tmpT.Rows[currRowIndex][count] = curr.ToString(); }
count++; } }
return tmpT;
} private static void SearchTable(ref int currRowIndex, Array ori, Array curr, ref DataTable table)
{ for (int i = ; i < curr.Length; i++)
{
bool isa = (curr as dynamic)[i] is Array;
if (isa)
{ SearchTable(ref currRowIndex, ori, (curr as dynamic)[i], ref table); }
else
{
if (table.Rows.Count < currRowIndex + )
{
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);
} try
{
table.Rows[currRowIndex][i] = (curr as Array).GetValue(i);
}
catch (Exception)
{ ;
}
if (i == curr.Length - )
currRowIndex++; } } }
static void Main(string[] args)
{
var t1 = ArrayConvert2DataTable(ReturnArray()); var t2 = ArrayConvert2DataTable(GetJCSZ());
Console.ReadKey();
} } }