简体   繁体   中英

{System.Byte[]} to System.Byte[]

I have an object with one of its properties being an array of bytes.

I assign that property to object called obj1 . When I assign that property to a variable, it acquires the {System.Byte[]} type which is an object, not an array.

As a result, I cannot access it. Problem is, whoever wrote the class did not define any .GetValue() method, so I cannot really access the elements, but the debugger watch shows the values!

The only methods defined for that object are Equals , GetType and ToString (which returns System.Byte and not the values).

I have tried the following without any luck:

var vals = (System.Byte[])obj1    //Returns a {System.Byte[]} object
var vals = (obj1 as System.Byte[])   //Returns a {System.Byte[]} object
var vals = obj1.GetValue           //Complains that GetValue is not defined

It is driving me crazy that I can see the array values using the watch window, but am having trouble accessing the elements.

Your first two assignments are both correct and you should be able to access the array or its elements by using vals[0] or vals.Length etc

The debugger will see all values, including private, inaccessible values that you can't reference through code. That's part of life in object oriented programming. If you're really trying to access inaccessible data, there are a few ways you could access this data in your program:

  • it's possible you can derive a class from the object you're trying to look at, and that class will have access to the hidden data. This will happen if the original code had the data member's protection level as protected .
  • You could use reflection to access the private data members, but that is far more advanced than I think what you're looking to do.

I wonder if you're simply looking for how to access data from the array? If you want to get the data out of the byte array, just do a for loop:

for (int i = 0; i < vals.Length; i++) {
    byte b = vals[i];
    // do something with b ...
}

You could also use foreach :

foreach (byte b in vals) {
{
    // do something with b ...
}

Check outMSDN's Arrays tutorial for more information on arrays. (Or just google arrays tutorial c# and pick your favorite reference site.)

I have this problem try something like this.

byte[] vByte = new byte[XLSStream.Length];
var stream = XLSStream.ToArray();
int count = 0;
foreach (var item in stream)
{
   vByte[count] = item;
   count++;
}

I had to get image stored in MS SQL database in image format. I used an SQL request for it but it returned me that image as object{System.Byte[]} . I found a function to convert it to byte[] :

byte[] ObjectToByteArray(object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }

But unfortunately the result was different from the original one and included 28 of extra bytes . I compared both of arrays and find that this function adds 27 bytes from start of array and 1 byte to the end of array. I converted byte[] to List and deleted the extra bytes:

byte[] obj = ObjectToByteArray(queryObject);
            List<byte> list = obj.ToList();
            list.RemoveRange(0, 27);
            list.RemoveAt(list.Count - 1);
            obj = list.ToArray<byte>();

I got an identical byte array as it stored in database. Then I converted it to an original image:

MemoryStream ms = new MemoryStream(obj);
            pictureBoxPhoto.Image = Image.FromStream(ms);
            pictureBoxPhoto.SizeMode = PictureBoxSizeMode.Zoom;

Maybe this way is specific but it works fine with images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM