This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM, that has a List<Action>. I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Actions have got correct [Serializable] and [NonSerializable] attributes.
The problem comes when serialization is attempted – it gives this error:
Type 'CASM.CASM+<>c__DisplayClass2c' in Assembly 'CASM, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' is not marked as serializable.
This <>c__DisplayClass2c is an autogenerated internal class that holds the different types of anonymous delegate I am using in my application. However, as we can see from the below image, it is not [Serializable]:
alt text http://bayimg.com/image/maebbaacj.jpg
What would be the best way to change my application so this does work? Make my own <>c__DisplayClass2c-type class and make it serializable? Or is there a better way?
EDIT: In the end I just made my own class, instead of the autogenerated one. I helps with debugging as well, actually having a descriptive name rather than just b__12().
It usually makes very little sense to serialize a delegate. Normally, you would choose to mark delegate fields as
[NonSerialized], and recreate it when needed. If your main intent is to store the delegates, then I would recommend thinking of a completely different approach, frankly.Additionally, note that
BinaryFormatteris brittle if you are planning to keep the data for any length of time (but acceptable for transient data)To look further, I suspect we’d need to look at some reproducible code.
Update: actually, I suspect you could serialize it by writing your own explicit capture classes (rather than the compiler-generated ones). But I still think the concept is fundamentally flawed. And writing capture classes by hand isn’t fun.
To address the points in comments; re long term storage – because it is so darned brittle – something as simple as changing from:
to
will destroy serialization; as will changing assemblies, type names, “looking at it funny”, etc.
Re the delegates; to give an example of a manual capture; instead of:
you might do:
with
As you can see – not always trivial (this is the simplest of examples).