convert any object to byte array

  • Thread starter Thread starter Abraham Andres Luna
  • Start date Start date
A

Abraham Andres Luna

does anyone know how to convert any type of object to a byte array
some code that doesn't work:

MailMessage mmSpec = new MailMessage("(e-mail address removed)", "(e-mail address removed)");
mmSpec.Attachments.Add(new Attachment(Server.MapPath("doc.pdf")));
mmSpec.Subject = "Test";
mmSpec.Body = "Hello";
mmSpec.IsBodyHtml = true;

MemoryStream msEmail = new MemoryStream((byte[])mmSpec);


i want to convert it to a byte array so i can save it to a file or sql
server, thank you.
 
If MailMessage is serializable you can use the serialization namespace to
get a stream that represents the object. Check online help for .NET
Serialization. Googling will also give you some helpful examples. That said,
I would think long and hard about storing a stream instead of the
primitives. Doing so will force you to deal with version tolerance.
Performance is of course an issue as well and will vary depending on your
database of choice.

Hope this helps,
Mike
 
Back
Top