473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C#-App: Inherit from ISerializable for Serialization

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
14 6277
vinci
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...

cheers!
Jan 12 '09 #2
vekipeki
229 Expert 100+
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
dantz
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
vinci
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
r035198x
13,262 8TB
What is the class type of the object you are serializing and what other classes does it inherit from?
Jan 13 '09 #6
dantz
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();
  5.  
  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. }
  17.  
  18. public class Balance
  19.     {
  20.         private long _currCredit;
  21.         private long _currGamePoint;
  22.         private Penalty _penalty = new Penalty();
  23.  
  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.     }
  40.  
  41. public class Penalty
  42.     {
  43.         private bool _isPenalty;
  44.         public bool IsPenalty
  45.         {
  46.             get { return _isPenalty; }
  47.             set { _isPenalty = value; }
  48.         }
  49.     }
  50.  
  51.  

thanks in advance
Jan 13 '09 #7
r035198x
13,262 8TB
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
dantz
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
vekipeki
229 Expert 100+
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
r035198x
13,262 8TB
@dantz
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
PRR
750 Expert 512MB
"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.
http://msdn.microsoft.com/en-us/library/system.serializableattribute(VS.71).aspx
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{}
  7.  
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.formatters.binary.bin aryformatter(VS.71).aspx
Or soap formatter http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapf ormatter(VS.71).aspx
Jan 13 '09 #12
mcan
2
I think I have to expand this question...

So I have a
[Serializible]
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??

//Markus
Jan 29 '09 #13
mcan
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)

//Markus
Jan 29 '09 #14
dantz
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..

@Markus

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

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

Similar topics

3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
1
by: Tamir Khason | last post by:
I have a base class MyBaseFoo with some properties, methods etc. I have some derived classes, implements small part of MyBaseFoo and a lot of their own. In order to ensure proper deserialization...
2
by: tony lock | last post by:
I have a class inherited from Control, which I want to serialize, since Control is not Serializable, I have had to implement ISerializable. This works but I now want to inherit this base class into...
5
by: Perecli Manole | last post by:
I have a class that has been serialized and saved to disk. I am trying to deserialize it back into the same class which now has an extra private member. It will not deserialize because its...
3
by: GreyAlien007 | last post by:
I extended the class TreeNode to add some properties of my liking. Anyway, no problems there, I can add my derived TreeNode into TreeNodeCollections and use the properties etc. However, when I...
1
by: ThunderMusic | last post by:
Hi, I have a class that must be serialized. One of it's properties cannot be serialized so I mark it with and replace it with a property I serialize "manually" (I put the elements in an array). I...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.