C#-App: Inherit from ISerializable for Serialization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantz
    New Member
    • Oct 2008
    • 71

    C#-App: Inherit from ISerializable for Serialization

    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.
  • vinci
    New Member
    • Aug 2006
    • 62

    #2
    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...

    cheers!

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      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.

      Comment

      • dantz
        New Member
        • Oct 2008
        • 71

        #4
        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

        Comment

        • vinci
          New Member
          • Aug 2006
          • 62

          #5
          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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            What is the class type of the object you are serializing and what other classes does it inherit from?

            Comment

            • dantz
              New Member
              • Oct 2008
              • 71

              #7
              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"

              Code:
              public class Player 
              {
                      private int _playerID;
                      private Balance _currentBalance = new Balance();
               
                      public int PlayerID
                      {
                          get { return _playerID; }
                          set { _playerID = value; }
                      }
                      public Balance CurrentBalance
                      {
                          get { return _currentBalance; }
                          set { _currentBalance = value; }
                      }
              }
               
              public class Balance
                  {
                      private long _currCredit;
                      private long _currGamePoint;
                      private Penalty _penalty = new Penalty();
               
                      public long Credit
                      {
                          get { return _currCredit; }
                          set { _currCredit = value; }
                      }
                      public long GamePoint
                      {
                          get { return _currGamePoint; }
                          set { _currGamePoint = value; }
                      }
                      public Penalty Penalty
                      {
                          get { return _penalty; }
                          set { _penalty = value; }
                      }
                  }
               
              public class Penalty
                  {
                      private bool _isPenalty;
                      public bool IsPenalty
                      {
                          get { return _isPenalty; }
                          set { _isPenalty = value; }
                      }
                  }

              thanks in advance

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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?

                Comment

                • dantz
                  New Member
                  • Oct 2008
                  • 71

                  #9
                  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.

                  Comment

                  • vekipeki
                    Recognized Expert New Member
                    • Nov 2007
                    • 229

                    #10
                    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) .

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by dantz
                      ....
                      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.
                      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.

                      Comment

                      • PRR
                        Recognized Expert Contributor
                        • Dec 2007
                        • 750

                        #12
                        "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 SerializableAtt ribute.
                        http://msdn.microsoft. com/en-us/library/system.serializ ableattribute(V S.71).aspx
                        If you intent to serialize a class that has object of another class you need to add SerializableAtt ribute to that class as well
                        Code:
                        [Serializable]
                        public class A{
                        public B b;
                        }
                        [Serializable]
                        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 http://msdn.microsoft. com/en-us/library/system.runtime. serialization.f ormatters.binar y.binaryformatt er(VS.71).aspx
                        Or soap formatter http://msdn.microsoft. com/en-us/library/system.runtime. serialization.f ormatters.soap. soapformatter(V S.71).aspx

                        Comment

                        • mcan
                          New Member
                          • Jan 2009
                          • 2

                          #13
                          I think I have to expand this question...

                          So I have a
                          [Serializible]
                          public class ExtendedList<My Class> : 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(Se rializationInfo info, StreamingContex t 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??

                          //Markus

                          Comment

                          • mcan
                            New Member
                            • Jan 2009
                            • 2

                            #14
                            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(Se rializationInfo info, StreamingContex t ctxt): base(info, ctxt)

                            //Markus

                            Comment

                            • dantz
                              New Member
                              • Oct 2008
                              • 71

                              #15
                              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..

                              @Markus

                              Thanks for the additional info..

                              Comment

                              Working...