如何检查对象是否包含字节数组?

时间:2022-12-23 07:30:57

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.

如果数据实际上是一个字节数组,我只想将数据分配给数组变量。

3 个解决方案

#1


6  

Try

尝试

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

#2


9  

How about this:

这个怎么样:

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

#3


1  

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.

使它成为类型对象(它通过Silverlight中的类型转换器进入)并且它起作用。

#1


6  

Try

尝试

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

#2


9  

How about this:

这个怎么样:

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

#3


1  

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.

使它成为类型对象(它通过Silverlight中的类型转换器进入)并且它起作用。