问题
I'm trying to serialize a type using the DataContractSerializer and am getting the exception below. This isn't for an SOA service, but I would still like to use the DataContractSerializer if possible. I am using .Net 3.5 SP1.
Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
回答1:
Can you post your class definition?
It seems like you are trying to serialize a class which has a field of type delegate, which I'm pretty sure will make the serializer choke on.
Did you decorate your class with the DataContract / DataMember attributes? In 3.5 SP1 there is a default behavior for the serializer that serializes everything public in a class by default if it is not marked with those attributes. Maybe you should explicitely mark each property that needs to be serialized with a DataMember attribute and leave out those that should not be.
Other than that, we would need to see your class definition for more help.
回答2:
There are 3 possible approaches to avoid the error described in http://blogs.microsoft.co.il/blogs/oshvartz/archive/2009/10/10/passing-event-handlers-over-wcf.aspx
来源:https://stackoverflow.com/questions/822082/why-cant-i-serialize-an-object-using-datacontractserializer