Serialize an instance

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

If I for example want to Serialize an object of this class Product se below.
Will this cause any problems because of all the members is private ?
In all the examples I have seen is all the members field public and that
is not what they normally are.

public class Product
{
private long Id;
private string Name;
private Price;

public Product(long id, string name, double price, string notes)
{
Id = id;
Name = name;
Price = price;
Notes = notes;
}

public override string ToString()
{
return string.Format("{0}: {1} (${2:F2}) {3}", Id, Nmae, Price,
Notes);
}
}

//Tony
 
Tony Johansson said:
If I for example want to Serialize an object of this class Product se
below.
Will this cause any problems because of all the members is private ?
In all the examples I have seen is all the members field public and that
is not what they normally are.

It depends on how you serilize the class. If you use the XmlSerializer,
the private fields will not be serialized, so it won't work for your class.
But if you serialize with a BinaryFormatter or a SoapFormatter, the private
fileds ARE serialized.
 
Back
Top