Marking Assembly Classes as Serializable!
Hi,
I am new to C# and the group! I am trying to use Serialization code to write a binary file for storing, but it is giving RunTime SerializationException: that some of the classes in an assembly are not marked as serializable. I am working with a code base for a project and have access to its code, so changed a number of classes by introducing [serializable] attribute. Now I am getting the same error for System.Timers.Timer, is that possible to mark it serializable, tried searching to get something but couldn't. Thanks for any help from the community!
Best,
Umar
Re: Marking Assembly Classes as Serializable!
Generally you'll want to keep your data separate from the UI components of the application (check out using any of the model-view architectures or MVC patterns).
Since the Timer is a UI component you shouldn't have to serialize that class. If you find you need to, then restructure your code, so the Timer class doesn't need to be serialized.
For example, say I have some text data that is bound to a textbox. If I need to serialize the data, I'll serialize the text property, but there is no need to serialize the TextBox class.
Re: Marking Assembly Classes as Serializable!
Thanks Ajay! Do you mean we need to mark the Timer object as a NonSerialiazable attribute? I am trying to do this, hope it works! I will give updates about it later, if it worked!
Re: Marking Assembly Classes as Serializable!
Quote:
Originally Posted by
Umar214
Thanks Ajay! Do you mean we need to mark the Timer object as a NonSerialiazable attribute?
No. You won't be able to mark the Timer as non serializable because you didn't write this code (it's part of the framework). As such, you won't be able to change the code.
The point I'm making is that you shouldn't be trying to serialize UI framework code anyway. You should only serialize data, not ui code.
Change your design and code so that the data is kept separate from the UI code, then serialize the data.
Re: Marking Assembly Classes as Serializable!
Dear Arjay!
Thanks for your earlier help, sorry for my stupid question, as am totally new, but devoted to learn.
I am using a code base of an Open community and the project called OpenSim is having a very large and complex code base thats not easy for me to change. I was suggested to use XmlSerializer class instead. I do undersatnd need to do changes but when I used a simple streamwriter to serialize to a file, this time, I got the following error:
21:39:19 - [BALANCER]:Entering ClientSerialization()
21:39:19 - [BALANCER]:Entering, to find out the src region!
a region is added
The port for this scene is 9000
Matched here,......
The number of avatars in the region Test1 are 1
21:39:20 - [BALANCER]:Entering the last, static method now!
21:39:20 - [BALANCER]:System.InvalidOperationException: There was an error refle
cting type 'OpenSim.Region.Framework.Scenes.ScenePresence'. ---> System.NotSuppo
rtedException: Cannot serialize member OpenSim.Region.Framework.Scenes.ScenePres
ence.KnownRegions of type System.Collections.Generic.Dictionary`2[[System.UInt64
, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[
System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c
561934e089]], because it implements IDictionary.
21:39:20 - [BALANCER]: at System.Xml.Serialization.XmlReflectionImporter.Impor
tTypeMapping(TypeModel model, String ns, ImportContext context, String dataType,
XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
Could you please guide me what I need to do, do I need to Customise XmlSerializer or need to write another version of the Class having only the status information.
Thanks alot and sorry for bothering you much!
Umar
Re: Marking Assembly Classes as Serializable!
I don't know what you need to do. Sorry.
Re: Marking Assembly Classes as Serializable!
Could you extract the data from the existing classes and put it into your own class. You could then serialise your own class.
If you want to use the XmlSerialiser, remember that only public properties/members will be serialised.
If you're not worried about reading the serialised data i.e. doesn't need to be xml then try the BinaryFormatter to do the serialisation. This is easy to use just by marking your class as serializable
Code:
[Serializable]
public class MyClass
{
private string myString = null;
public string MyString
{
get
{
return this.myString;
}
set
{
this.myString = value;
}
}
}