Convert arraylist object to byte[]

旧巷老猫 提交于 2019-12-24 16:26:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!