views:

595

answers:

6

Hi,

I have a situation where I need to serialize an object but don't want to serialize any of its references. This is because I don't know in advance which dlls the object might be referencing and therefore can't ensure that they are serializable objects. This has arisen from needing to serialise plugins to preserve their state.

Am I right in thinking that this is the case with XML serialization (shallow)? But that this will ignore anything private in the object - which isn't what I want?

Is this somehow possible?

A: 

If you know at the type declaration time which references should not be serialized you can use binary serialization and filter out members with the [NonSerialized] attribute.

JaredPar
+1  A: 

Xml Serialization will only work on things that are publicly accessible. Also, unless you mark a public property / field with the [XmlIgnore] attribute, it will be serialized.

If you're just looking at some method of serialization, then use binary serialization. It will serialize the internal state of the object (all fields, private or otherwise). You can use the [NonSerialized] attribute to ignore specific references if you want.

Nader Shirazie
The issue is that I won't be writing the plugins myself and thus won't be able to mark specific references...
Ataris22
So you're not writing the plugins, but you need to serialize them? Or do you only need to serialize the plugin host (which you are writing)?
Nader Shirazie
A: 

Put NonSerialized attribute in case of binary and XmlIgnore attribute in case of xml serialization to reference properties or fields

ArsenMkrt
A: 

You do know which properties you can serialize, though, correct? Are these plugins implementing a common interface? If that is the case, you should be able to write a generic serializer that will only serialize the specific properties that you choose.

Here is a basic example that will give you the idea of what you need to do: Object Serialization using C#

If you are just looking to serialize native types within your class instances, you should just be able to implement ISerializable, though, and decorate the properties that you do not want to be serialized.

joseph.ferris
It's the methods in the plugins that they write that are my concern. If somebody writes a plugin and it calls a third party dll then it will try and serialize that dll wont it?
Ataris22
Without reflection, not easily. Are they exposing public properties? What I have done is to create a "StateBag", where you basically store a hash of items. Something like "SetProperty", "GetProperty" and they end up in a common backed field that can be serialized. I wouldn't recommend that, as I do not like creating a get/set method to accomplish something like that, though.
joseph.ferris
A: 

You can try something like this:

Type myType = currentObject.GetType();

Then check to see if the object is serializable by using:

myType.IsSerializable; //returns a bool

That should tell you whether or not the object is serializable. If you really need to know whether every single object inside of a class is serializable, such as other nested classes or custom types, then you could probably use reflection to read each object, use the code above, and verify whether or not it is serializable. This, however, might be a more complicated approach, and may not be plausible, especially if you have overhead issues to deal with.

Zensar
Do you happen to know how other plugin frameworks handle this issue? It seems like it is something that must come up frequently
Ataris22
This is a tricky question. Really, if the plugin is meant to be used in the way you're intending, then the option for serialization should be contained within the plugin, and this shouldn't be an issue. If it isn't, then you need to ask yourself if you are using the plugin in the correct manner, or if this is the proper plugin to use. If it is utterly required that you use these plugins, even though they lack the proper serialization functions, then you may have to adapt your approach. You could always write some sort of wrapper to take care of these issues.
Zensar
A: 

It may be useful for you to separate what you want to persist from how you persist it.

It seems like you want control over how you want to persist data, but obviously, cannot know what it is, because of your plugin model.

One scheme that may make sense to you is to give your plugins some sort of object or interface they can write to and read from when its time to save / load. Its fine to document these constraints.

For example, when persisting, allow your plugins to pass to you:

  1. some arbitrary byte array which they are responsible for serializing / deserializing. Then it is a plugins responsibility to make sure they use objects that are appropriately serializable.
  2. a dictionary of strings
  3. an xml file
  4. others...

Store this information per plugin (in whichever form you want), and loading up again, pass back the same information.

This is simply an approach around the fact that in the end, the plugin knows what it needs to save, and needs to own that piece of information.

Nader Shirazie