WCF Operations and Multidimensional Arrays

Posted by JoshReuben on Geeks with Blogs See other posts from Geeks with Blogs or by JoshReuben
Published on Wed, 16 Mar 2011 10:31:32 GMT Indexed on 2011/03/16 16:11 UTC
Read the original article Hit count: 328

Filed under:

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!

© Geeks with Blogs or respective owner