2

Hello I'm new to C# and I'm trying to serialize some data. I have a base class, which implements ISerializable and more subclasses that extend the base class. In my base class I wrote this:

protected BaseClass(SerializationInfo info, StreamingContext context) { if (info == null) throw new System.ArgumentNullException("info"); } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) throw new System.ArgumentNullException("info"); } 

The problem is that when I run my program I get this error saying that my subclasses are not marked as serializable. It's a big project. I bassicaly have a student class class Student : ISerializable, which is the base class, and I have GraduateStudent : Student and PhDStudent : Student. When I try to serialize a list of students( can be Student, GraduateStudent or PhDStudent) I get the error above. I have also tried to write like this PhDStudent : Student,ISerializablebut no success

6
  • 6
    Then mark as [Serializable] :) Commented Nov 27, 2013 at 19:47
  • I've done that but I get the same error Commented Nov 27, 2013 at 19:50
  • We are only able to work with the information and example given. Please include that in the above example. Commented Nov 27, 2013 at 19:52
  • 2
    The message is clear enough: mark the subtypes. However: I strongly suggest to you that BinaryFormatter is going to hurt you here Commented Nov 27, 2013 at 19:56
  • I have written more info about it and updated the question Commented Nov 27, 2013 at 19:59

2 Answers 2

1

If it is complaining that they aren't marked as serializable: then do that. Every type in the model (not just the base-type) must be marked [Serializable]. Actually, it is very rare that you would need to implement ISerializable here - I strongly suggest you don't do that, and just let the serializer worry about the fields until you know exactly what you are doing...

...because: when you are familiar with it, you'll probably find a lot of reasons to not blindly use BinaryFormatter - it can be horribly brittle and versions intolerant. Unless this is for a "needs to run once only" thing, I would strongly suggest alternatives like XmlSerializer, DataContractSerializer, json.net or protobuf-net.

Sign up to request clarification or add additional context in comments.

Comments

0
public abstract class BaseClass { } public class Child1:BaseClass { } public class Child2:BaseClass { } public class SeriliazerTest { // You have to define them here, otherwise they will not be found [XmlArrayItem(Type = typeof(Child1), ElementName = "Child1")] [XmlArrayItem(Type = typeof(Child2), ElementName = "Child2")] public BaseClass[] Child {get;set;} } 

8 Comments

ISerializable and the exception message suggest BinaryFormatter. Actually, this won't even work correctly in XnlSerializer - it is missing the XmlIncludeAttribute markers
Thanks @MarcGravell Sorry I do not saw that he want BinaryFormatter today is bad day for me I cant get any scores lol
Actually I never meant to get so complicated. I only want to serialize my list of students. In java I made this very easy but C# is giving me headaches
@user3043278 Then why you do not just use the XML serilizer very easy and human readable shall I post for you an example.
This is what I found on msdn so I thought it would work. Perhaps a simple example would help
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.