After reading again your question, if the object is already a byte[]
simply cast it:
object yourObject = xxx();
byte[] data = (byte[])yourObject;
bool hasData = data != null && data.Length > 0;
質問
I want to convert an object, to a byte with silverlight.
So, first I found this : convert object(i.e any object like person, employee) to byte[] in silverlight
But this is not working(any of the answers), the dll proto seems not to be good.
Plus, i tried this:
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Address));
StringBuilder stringBuilder = new StringBuilder();
using (StringWriter writer = new StringWriter(stringBuilder))
{
serializer.Serialize(writer, address);
}
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] data = encoding.GetBytes(stringBuilder.ToString());
But I got a result in the byte.
What I receive is an object byte[0]
, and I just want to verify his lenght is 0 or more.
I cannot do tihs with an object
, and that's why I tried to convert it. But the byte in result is different from byte[0]
How can I do what I want? Just a comparison to 0.
解決
After reading again your question, if the object is already a byte[]
simply cast it:
object yourObject = xxx();
byte[] data = (byte[])yourObject;
bool hasData = data != null && data.Length > 0;
他のヒント
If you like there is also the MemoryStream
object which convert object
to byte[]
and byte[]
to object