Serialization does work in sample project, but not in app
In an app I am developing, I found a problem when serializing a custom class.It's a class derived from a Generic Tree cl开发者_运维技巧ass, so let's call it DerivedFromTree. The Tree class is in another assembly.
[Serializable]
class DerivedFromTree : Tree<UnderlyingTreeType>
{
...
}
To isolate my problem I created a new console project, added the DerivedFromTree files and its dependencies and added a reference to the DLL in which Tree resides. Then added the following code:
DerivedFromTree dft = new DerivedFromTree("label");
UnderlyingTreeType utt = new UnderlyingTreeType(...);
dft.AddChild(utt);
utt = new UnderlyingTreeType(...);
dft.AddChild(utt);
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dft);
ms.Position = 0; // position stream to 0
DerivedFromTree dft_result = (DerivedFromTree)bf.Deserialize(ms);
After running this code in the sample project, dft_result contains a valid DerivedFromTree with its children. However, if I put the same code in my original application, I get an exception when calling bf.Serialize:
{System.Runtime.Serialization.SerializationException: Type 'MyAssembly.Tree`1[[UnderlyingTreeType, Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' from assembly 'Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
in System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
in System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
in System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
in System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
in System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
in System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
in Class.Function() in C:\...\FileName.cs:line XXX}
Any idea about what could be happening there?
Update: The classes Tree, DerivedFromTree, UnderlyingTreeType and all its dependencies are marked as [Serializable]. Failing to do so would result in the small console project throwing an exception. If I remove the [Serializable] attribute from Tree class, the console project throws the exact same error as the original app.
Perhaps your object has delegate fields which may or may not throw an exception when being serialized.
Make sure you mark delegates nonserializable:
[Serializable]
public class MyClass
{
[NonSerialized]
EventHandler m_MyEvent;
}
For events, you should specify field
attribute qualifier when applying NonSerialized
:
[Serializable]
public class MyPublisher
{
[field:NonSerialized]
public event EventHandler MyEvent;
}
Don't forget that SerializibleAttribute
has it's IsInherited
property set to false
. If your code looked exactly as posted above (without decorating DerivedFromTree
by [Serializible]
) it would not work...
Request you to go through the section of
Steps in the Serialization Process
Serialization Guidelines
of article Object Serialization
http://msdn.microsoft.com/en-us/library/ms973893.aspx
and check if you haven't missed anything
The problem is fixed now. I removed the reference to the DLL containing the Tree class and added it again, and suddenly everything worked.
I think I'm going to delete the question, as it doesn't seem very useful...
Thank you all for your help.
精彩评论