By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

C#-App: Inherit from ISerializable for Serialization

P: 71
hi everyone,

when doing serialization, do I really need to inherit from ISerializable ?
I am not sure if there is something wrong with my code but when i don't inherit from that interface I can still serialize my object and deserialize it.
Am I missing something?

Thank you in advance.
Jan 12 '09 #1
Share this Question
Share on Google+
14 Replies

P: 62
i dont think so... you dont need to inherit that interface...
Just by placing
<Serializable()> for VB
[Serializable()] for C#
on top of a class, will make it serializable...

I think you inherit that interface when you need to serialize a type/property which is not serializable...

Jan 12 '09 #2

Expert 100+
P: 229
You need to implement ISerializable only if you need to create your own, custom serialization. If .Net serialization is ok for you, just add [Serializable] and you're fine.
Jan 12 '09 #3

P: 71
Ok, now I understand. But if I implement ISerializable will I be able to control the size of the serialized object?

Thanks for the replies
Jan 13 '09 #4

P: 62
No, ISerializable can't control the size of your object. It only helps you add your object (which is not serializable) to the entire group of serializables if i may say, so that object may be accessible when you deserialize.
Jan 13 '09 #5

P: 13,264
What is the class type of the object you are serializing and what other classes does it inherit from?
Jan 13 '09 #6

P: 71
How to know which is not serializable? because on my class(Please see code below)
I tried serializing it and it works. I am not sure if there are any drawbacks if the my defined classes are the kinds of "not serializable object"

Expand|Select|Wrap|Line Numbers
  1. public class Player 
  2. {
  3.         private int _playerID;
  4.         private Balance _currentBalance = new Balance();
  6.         public int PlayerID
  7.         {
  8.             get { return _playerID; }
  9.             set { _playerID = value; }
  10.         }
  11.         public Balance CurrentBalance
  12.         {
  13.             get { return _currentBalance; }
  14.             set { _currentBalance = value; }
  15.         }
  16. }
  18. public class Balance
  19.     {
  20.         private long _currCredit;
  21.         private long _currGamePoint;
  22.         private Penalty _penalty = new Penalty();
  24.         public long Credit
  25.         {
  26.             get { return _currCredit; }
  27.             set { _currCredit = value; }
  28.         }
  29.         public long GamePoint
  30.         {
  31.             get { return _currGamePoint; }
  32.             set { _currGamePoint = value; }
  33.         }
  34.         public Penalty Penalty
  35.         {
  36.             get { return _penalty; }
  37.             set { _penalty = value; }
  38.         }
  39.     }
  41. public class Penalty
  42.     {
  43.         private bool _isPenalty;
  44.         public bool IsPenalty
  45.         {
  46.             get { return _isPenalty; }
  47.             set { _isPenalty = value; }
  48.         }
  49.     }

thanks in advance
Jan 13 '09 #7

P: 13,264
Since you haven't marked that class as [serializable], you can only do XML serialization on that class. To be able to to serialize it using the BinaryFormatter you must mark it serializable.
How did you serialize it?
Jan 13 '09 #8

P: 71
I already marked it as serializable in the VS. I just had that code(w/o Serializable) from my old code.

What would be the difference between [serializable] and [serializable()] ???

And one more thing, you said that i can do xml serialization in the class that I posted, is it possible to make my objects serializable in xml and at the same time in binary? What's the one I read about xml serialization that you still need to identify which will be attributes and elements?

Thanks for the replies.
Jan 13 '09 #9

Expert 100+
P: 229
You don't have to worry about your class being "not serializable", you want to implement ISerializable only if you want to customize serialization.

For example, you have a Singleton object (the one and only, static), referenced many times in your class. If you use native serialization (you simply add the [Serializable] attribute), every reference to that Singleton will be serialized as a new object. When deserializing, every reference will point to a new object, meaning that you will get a bunch of new copies of your "Singleton".

To avoid this, you can implement ISerializable for your Singleton class, to ensure that each deserialization of a Singleton object returns the same reference to your static object.

Check MSDN for an example: ISerializable Interface (System.Runtime.Serialization).
Jan 13 '09 #10

P: 13,264
All objects are already XML serializable. You need to identify which will be attributes and which will be elements if you don't like the default XML file that is produced for your object.
Jan 13 '09 #11

Expert 100+
P: 750
"when doing serialization, do I really need to inherit from ISerializable?"
No. If a class needs to control its serialization process, it can implement the ISerializable interface.
Any class that might be serialized must be marked with the SerializableAttribute.
If you intent to serialize a class that has object of another class you need to add SerializableAttribute to that class as well
Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2. public class A{
  3. public B b;
  4. }
  5. [Serializable]
  6. public class B{}
For XML Serialization the class needs to be public... It serializes public fields only XML Serialization in the .NET Framework
You could also use BinaryFormatter aryformatter(VS.71).aspx
Or soap formatter ormatter(VS.71).aspx
Jan 13 '09 #12

P: 2
I think I have to expand this question...

So I have a
public class ExtendedList<MyClass> : List<MyClass>

The ExtendedList class contains 2 extended variables which I don't need to save (I'm not saving to XML, just using the binaryFormatter). So I thought that I somehow just could call the base function

public ExtendedList(SerializationInfo info, StreamingContext ctxt): base()

But it seems to me that the data never is fetched (I believe that the data is saved because when I step through the save(serialize) section the MyClass GetObjectData is run.

Does anyone have a solution or tip on this??

Jan 29 '09 #13

P: 2
Ok, found it!

For those who might be interested:

I didn't know that you called the base constructor WITH the parameters!
So just do it like this:

public ExtendedList(SerializationInfo info, StreamingContext ctxt): base(info, ctxt)

Jan 29 '09 #14

P: 71
hey, I forgot to thank all of people who replied on my thread.
Thanks for your replies. I understand serialization now and implementing it already. Both the XML and BinaryFormatter have worked for me..thanks..


Thanks for the additional info..
Jan 30 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.