问题描述
我有一个 C# WCF webservice,它被两个 VB 6 项目调用.目标 VB 项目正在向客户端 VB 项目发送一个多维数组.
I have a C# WCF webservice which is called by two VB 6 project. The target VB project is sending to the client VB project a multidimensional array.
我想将多维数组转换为锯齿状数组,但我没有运气.
I want to convert the multidimensional array to a jagged array but i have no luck.
如何在对象[,] 中找到能够初始化锯齿状数组的元素数量?
How can i find the number of olements in my object[,] to be able to initialize the jagged array ?
我想按照这个问题的答案但我的对象上没有 GetLength
方法.
I want to follow the answer from this question but i don't have a GetLength
method on my object.
我试过:
int firstElement = astrManTfrLetters.GetLength(0);
int secondElement = astrManTfrLetters.GetLength(1);
然后我就卡在这里了.
推荐答案
通常所提出的解决方案假设基于 0 的索引,但情况并非总是如此,主要是如果您在客户端上处理 object[,]
用于 Microsoft Excel.
Usually the solutions presented assume 0-based indices but that's not always the case, mainly if on the client you are dealing with object[,]
's for Microsoft Excel.
这是任何索引的解决方案:
Here is a solution for any indices:
internal static class ExtensionMethods
{
internal static T[][] ToJaggedArray<T>(this T[,] twoDimensionalArray)
{
int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0);
int rowsLastIndex = twoDimensionalArray.GetUpperBound(0);
int numberOfRows = rowsLastIndex + 1;
int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1);
int columnsLastIndex = twoDimensionalArray.GetUpperBound(1);
int numberOfColumns = columnsLastIndex + 1;
T[][] jaggedArray = new T[numberOfRows][];
for (int i = rowsFirstIndex; i <= rowsLastIndex; i++)
{
jaggedArray[i] = new T[numberOfColumns];
for (int j = columnsFirstIndex; j <= columnsLastIndex; j++)
{
jaggedArray[i][j] = twoDimensionalArray[i, j];
}
}
return jaggedArray;
}
}
这篇关于在 C# 中将多维数组转换为锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!