问题
I have araylist object named list, here i want to convert my arraylist object to byte[], while doing the below coding its giving an error: "At least one element in the source array could not be cast down to the destination arraytype" my coding:
byte[] obj= new byte[list.Count];
list.CopyTo(obj);
here my array list objects returns report data from ssrs reports.what i want to do here to solve this issue.
回答1:
Look at this (this will show you where can be a problem)
List<object> list = new List<object>();
list.Add((byte)1);
list.Add((byte)0);
list.Add("wtf");
list.Add((byte)1);
byte[] obj = list.OfType<byte>().ToArray();
if(obj.Length!=list.Count)
{
//Exception, not all objects in list is 'byte'
}
回答2:
If you mean the ArrayList contains items of type byte
then use such syntax:
byte[] obj = (byte[])list.ToArray(typeof(byte));
Otherwise what you want is not clear, as objects can't be cast to byte just like that so please explain better.
来源:https://stackoverflow.com/questions/10578310/convert-arraylist-object-to-byte