Converting instances to byte[]

C

Curious

Hi,

I have the following class, and I will be creating instances of it to
transfer it over a networkstream. Now I need to convert instances p1,
p2 and p3 to byte[].

How can I do this?

Can someone help me out
Thanks in Advance

Person p1 = ("MyName", "MySurname");
Person p2 = ("MySurname");
Person p3 = ("MyName");



public class Person
{
private string name, surname;

public Person(){}
public Person(string n) {Name = n;}
public Person(string s) {Surname = s;}
public Person(string n, string s)
{
Name= n;
Surname = s;
}

public string Name
{
set {name = value;}
get { return name;}
}

public string Surname
{
set {surname = value;}
get {return surname;}
}
}
 
M

Markus Stoeger

Curious said:
Hi,

I have the following class, and I will be creating instances of it to
transfer it over a networkstream. Now I need to convert instances p1,
p2 and p3 to byte[].

How can I do this?

Have a look at the BinaryFormatter and the XmlSerializer classes. The
BinaryFormatter creates a binary stream of your objects, the
XmlSerializer creates XML from your objects, which you can send over the
network as well.

hth,
Max
 

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