convert any object to byte array

  • Thread starter Abraham Andres Luna
  • 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.
 
M

Mike

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top