Stephen Price
Stephen Price

Reputation: 1649

How do I check if an object contains a byte array?

I'm having an issue with the following code.

byte[] array = data as byte[]; // compile error - unable to use built-in conversion

if (array != null) { ...

I only want to assign the data to array variable if the data is actually a byte array.

Upvotes: 10

Views: 18887

Answers (3)

Joel Etherton
Joel Etherton

Reputation: 37533

Try

if(data.GetType().Name == "Byte[]") 
{
    // assign to array
}

Upvotes: 11

Upul Bandara
Upul Bandara

Reputation: 5958

How about this:

byte[] array = new  byte[arrayLength];
if (array is byte[])
{
    // Your code
}

Upvotes: 18

Stephen Price
Stephen Price

Reputation: 1649

As soon as I asked this I realised that the type of data was not object.

Making it of type object (its coming in via a type converter in Silverlight) and it worked.

Upvotes: 1

Related Questions