WCF Operations and Multidimensional Arrays
- by JoshReuben
You cant pass MultiD arrays accross the wire using WCF - you need to pass jagged arrays.
heres 2 extension methods that will allow you to convert prior to serialzation and convert back after deserialization:
public static T[,] ToMultiD<T>(this T[][] jArray)
{
int i = jArray.Count();
int j = jArray.Select(x => x.Count()).Aggregate(0, (current, c) => (current > c) ? current : c);
var mArray = new T[i, j];
for (int ii = 0; ii < i; ii++)
{
for (int jj = 0; jj < j; jj++)
{
mArray[ii, jj] = jArray[ii][jj];
}
}
return mArray;
}
public static T[][] ToJagged<T>(this T[,] mArray)
{
var cols = mArray.GetLength(0);
var rows = mArray.GetLength(1);
var jArray = new T[cols][];
for (int i = 0; i < cols; i++)
{
jArray[i] = new T[rows];
for (int j = 0; j < rows; j++)
{
jArray[i][j] = mArray[i, j];
}
}
return jArray;
}
enjoy!