Serialize List<Func<T>> - c#

I have a class which contains a List<Func<T>>. The class is serializable and will be transferred over WCF. Now, I have problems with contained List<Func<T>>. What can I do that this list will also be serializable?

How would you expect a Func<T> to be serialized? It's a pointer to a function - such a thing cannot be serialized.

Related

Deserialize XML to a list of objects that share the same base class

I'm using c#. I have a base class, we'll call BaseClass. I have three other classes that derive from BaseClass. I have a List<BaseClass> that contains different instances of the derived classes. I serialize this list to XML using the XmlSerializer and it looks fine.
However, when I try to deserialize the XML back to the objects, I'm getting XML exception errors thrown pointing to the first object.
How can I deserialize this list of objects? When I serialize it, I pass the possible types, do I need to set this when I deserialize so the deserialize method knows what objects it could possibly have to create? Is there another way? Is it possible?
You will need to use the XmlInclude attribute to specify all the subclasses of the base class. See How to XML serialize child class with its base class or http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute(v=vs.110).aspx

C#: Which is an already serializable collection (I mean, created from microsoft)

I need to serialize a collection, but I would like to know if there is any already serializable collection before taking code from third parts or write it by myself. I already implemented some serializable collection, but this is a stupid situation where I just need to pass an array of serializable classes to the clipboard and back from it (copy/paste).
Any suggestion on what should I use?
Any class in the System.Collections or System.Collections.Generic namespace should be serializable. However, this doesn't mean that the content (or in the case of generics, T is serializable). This is visible with Dictionary<K,V>, as KeyValuePair<K,V> isn't directly serializable.
I used List<T> before and it works great.

Adding a method to an object that is serialized, but it doesnt' need to be serialized

I have an object that is currently serialized, and I want to do add a method to this class. The method shouldn't be serialized as it doesn't hold any state or anything, but it will modify the classes data but this doesn't need to be serialized (it will parse the value of a serializable string property).
How can I add this method? Or should I have this method on another utility class instead?
Methods aren't serialized. Only properties and fields are serialized. And Properties are only eligible for serialization if using DataContractSerializer (BinaryFormatter will only serialize Properties' underlying fields).
Adding this method won't be a problem, methods aren't serialized.
If you have access to the class then as I understand it serialization deals with properties and fields. Methods don't need to be and don't get serialized. They process the data
Quick answer: Most cases shouldn't affect unserialize an object.
Only fields are serialized. Methods and properties arent (properties are methods). The XmlSerializer is an exception; it only looks at public properties, and those must be public get / set if you are to deserialize them.

Serialize/deserialize objects - order of fields matters?

Is it possible that DataContractSerializer wrongly deserializes an object if the fields are not in the "correct" (whatever that means) order?
The classes that I try to serialize/deserialize do not have order-attributes placed on fields/properties. Yet one of my fields always gets deserialized as null even though it has a non-null value (it actually contains a list of strings).
When I moved two XML elements in serialized file around to match the order in another XML example (for which deserialization worked without problems) everything started to work.
This makes no sense to me but maybe someone knows better. ;)
To be eligible for correct serialization / serialization by the DataContractSerializer, all of the following must be true.
The class that must be serialized must have SerializableAttribute or DataContractAttribute set;
The properties and fields of the class that must be serialized require the DataMemberAttribute set;
The datatype of the serializable property or field must be serializable (i.e., have a public ctor and inherit ISerializable);
The class that must be serialized must implement IExtensibleDataObject;
Note: serializable fields can be either public or private.
Members must be in alphabetical order or you should use the Order-property of the DataMemberAttribute.
So, the order of the declaration does matter. If members are not in alphabetical order, they are skipped. Look up this answer at StackOverflow for an example, perhaps it applies to your case.

Interface "not marked with serializable attribute" exception

I have a very odd exception in my C# app: when trying to deserialize a class containing a generic List<IListMember> (where list entries are specified by an interface), an exception is thrown reporting that "the type ...IListMember is not marked with the serializable attribute" (phrasing may be slightly different, my VisualStudio is not in English).
Now, interfaces cannot be Serializable; the class actually contained in the list, implementing IListMember, is [Serializable]; and yes, I have checked that IListMember is in fact defined as an interface and not accidentally as a class!
I have tried reproducing the exception in a separate test project only containing the class containing the List and the members, but there it serializes and deserializes happily :/
Does anyone have any good ideas about what it could be?
Edit:
We are using a BinarySerializer; and I repeat, when extracted to a test project the class serializes happily. So I do not need a workaround to serialize a class containing a List<IThing>, as in general this works fine (as long as the actual classes implementing IThing are serializable); what I am looking for is reasons why it might not work this particular time...
I have now put in a workaround (serializing each list member individually, together with the number of entries, and recreating the List by hand), but would really like to find out what it could be for future reference.
It doesn't matter that the class backing the interface is serializable. Interfaces cannot be serialized, period.
In order to deserialize, the serializer needs to be able to instantiate a concrete type, and it determines this type by reflecting on the fields/properties of the class-to-be-deserialized.
If the type of one of those properties is an interface, then it will never be able to construct a concrete type to assign to that member. All it sees is the interface, it has no idea which class originally implemented it when the data was serialized.
If you want the class to be serializable, then every class in the object graph must be a concrete type. No interfaces allowed.
(Postscript: Actually, I sort of lied, the BinaryFormatter can serialize/deserialize directly to/from interface types, but I strongly suspect that's not what's being used here.)
The Easy Way (Although ugly) Wrap your list:
public ListMemberCollection : List<IListMember>, ISerializable
{
// Implement ISerializable Here
}
The Alternative Way (Although better) AbstractBaseClass:
[Serializable]
public ListMemberBase : IListMember
{
// Implement abstract versions of everything
}
A possibility?: (On your other class)
class TheClassYoureSerializing
{
[Serializable]
public List<IListMember> list { get; set; }
}

Categories