Passing array list by parameter

Asked

Viewed 112 times

1

I have the following problem:

I need to pass some files (xmls and pdf) from my winforms application to a webservice.

The problem is that it is not possible to pass via parameters to a WS data type like List<>.

In this case, I will have it passed array containing the bytes of the file, so I can render it on the server inside the WS.

I’m having trouble sending and receiving this list through a list.

// Cada arquivo eu insiro em um List<byte[]>, exemplo:
listaDeBytes.Add(File.ReadAllBytes(arrayAnexos[x]));

In this case, I have in my list 3 records with their respective bytes inside. To transform this list into an array, I do the following:

// Transformando a List<byte[]> em uma array de byte:
byte[] listaEmByte = listaDeBytes.SelectMany(a => a).ToArray();

At this point, he inserted the 3 records with their bytes in this array, but it seems to have put everything together. The problem is how to interpret this EmByte list that I get on my WS. How to turn it back into a list of bytes for me to read it.

I tried to turn it into a byte[][], but it is not possible to convert list to byte[][] through Selectmany.

Any help is welcome. Thank you.

1 answer

0


The syntax to convert your byte array list to a two-dimensional byte array should be this:

byte[][] listaEmByte = listaDeBytes.SelectMany(a => new byte[][]{a}).ToArray();

Or if you want to remove redundant code, we can take the part that explicitly creates the array.

byte[][] listaEmByte = listaDeBytes.SelectMany(a => new[] { a }).ToArray();
  • 1

    Thank you very much Murari. Really this way transformed into two-dimensional. And this solved the problem! Thank you for your help =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.