Exclude parent object on binary serialization in C#

I have a question about binary serialization in C#

I need to be able to deep clone objects of class B (along with all it's subobjects in the graph of course). I'd like to implement this by using binary serialization. The discussion if that's the best method is irrelevant in the context of this question.

Say I have this class structure:

public class A { private B objB; } [Serializable] public class B : ICloneable { private C objC1; private C objC2; public object Clone() { B clone = Helper.Clone<B>(this); return (B)clone; } } [Serializable] public class C { int a; int b; }

The helper class for deep cloning with binary serialization (I got this method code from somewhere on the net, don't really remember where TBH, but it looks alright)

public static class Helper { public static T Clone<T>(T OriginalObject) { using (Stream objectStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(objectStream, OriginalObject); objectStream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(objectStream); } } }

So at some point in my app, I hit following code:

B clone = (B)objA.objB.Clone();

The problem is that the debugger moans about class A not being marked as Serializable.

But I don't want to serialize A, I want to serialize B and it's subobject C.

I guess it tries to serialize the parent object A too, because it's all interconnected in the object graph. But is there any way I can exclude it from being serialized at this point?


This quick mock code works perfectly fine, your problem is elsewhere, perhaps C has variable of class A ?

public class A
private B bObj = new B();

public A()
B copy = bObj.Clone() as B;
public class B : ICloneable
private int test = 10;

public object Clone()
return Helper.Clone(this);


EDIT per comment:

please add the following to your event in B


This will prevent serialization of the invocation list of the event which in turn references A

If B does not hold any reference to A, serializing B will not require A to be serializable even if A has a reference to B.

From your code sample, B does not hold any reference to A so it should work. Are you sure that the code sample you provided really represent your real scenario?

Im not sure, but what if you mark it as nonserializable? I'd test that :P

There must be something else in play here - your code compiles and executes without issue (as it should - type A is not a part of type B's object graph).

Is it possible that objB is actually an instance of type A?

Ok thanks for all the replies, I did find the issue ...

Class B contains a public event field, and an eventhandler in Class A is subscribed to it. If I unsubscribe the eventhandler, serialization works.

My mistake for not posting the entire code situation, sorry :(

Category:c# Time:2010-01-12 Views:0

Related post

  • .Net Binary Serialization - How to Limit the Object Graph? 2009-03-18

    I have an object that I'd like to serialize and distribute between processes as efficiently as possible. The object itself has a reference to another object like so: public class Foo { // Unique Identifier: public int Id; public Bar Bar; } public cla

  • Binary serialization of Silverlight XAML object 2009-04-06

    I'm working on Silverlight application that needs to display complex 2d vector graphics. It downloads zipped XAML file from the server, parses it (XamlRead) and injects to the layout root on the page. This works fine for fairly small xaml files. The

  • Why does Binary Serialization require the object to be marked as serializable? 2009-10-20

    I was using the xml serializer but when I switched to binary serialization, it throws an exception: -- Runtime error: dotNet runtime exception: Type 'MyTypes.MyObject' in Assembly 'MyTypes, Version=, Culture=neutral, PublicKeyToken=null' is no

  • C# Object Binary Serialization 2009-11-17

    I want to make a binary serialize of an object and the result to save it in a database. Person person = new Person(); person.Name = "something"; MemoryStream memorystream = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(

  • .NET: How to binary serialize an object with attribute [DataContract]? 2010-03-04

    Class marked as [DataContract] can't be ISerializable at the same time. OK, so how can I serialize this type of object to a binary stream? private byte[] GetRoomAsBinary(Room room) { MemoryStream stream = new MemoryStream(); BinaryFormatter bf = new

  • Silverlight Object Binary Serialization to Database 2010-06-22

    Right now I am programming a multiplayer card game in Silverlight I am wondering how to store a object best binary in a sql database. I have a GameState Object which contains the current state of a game between two players. I want to store this objec

  • .NET Binary Serialize object with references to other objects . . . what happens? 2011-02-05

    If you have an object instance A that references other objects (for example instances B and C), and you binary serialize A to a file, what happens? Do you now have serialized data that includes A, B and C? How does it work exactly? What will I get if

  • Easiest way to serialize a binary serializable object to xml in .Net 2011-03-04

    I have no idea why this is so difficult to find. I have a fairly simple object that is currently binary serializable. I am using NHibernate to serialize it and store it in a single table in the database. I have now received a further requirement that

  • Exclude an object during serialization in XmlSerialize 2011-03-07

    I'm attempting to serialize an object but I would like to exclude one of the objects inside it. I've tried [NonSerialized] and it is still attempting to serialize it when I call XmlSerialize on a Cart object. [Serializable] [XmlRoot("Cart")] public c

  • Create pointer to parent object in deserialization process in C# 2008-12-09

    I have a classes like: [Serializable] public class child { public Parent parent; } [Serializable] public class Parent { public List<child> children; } When I deserialize Parent, I want each of each children to have a reference to it's parent. Q

  • Strange behaviour of .NET binary serialization on Dictionary 2009-01-19

    I encountered a, at least to my expectations, strange behavior in the binary serialization of .NET. All items of a Dictionary that are loaded are added to their parent AFTER the OnDeserialization callback. In contrast List does the other way. This ca

  • Serializable objects Vs Non Serializable objects 2010-09-10

    what is the differnce Serializable objects vs non serialivable objects, And what makes an object serialiable? And What does serializable objects have to do with xml? Thanks --------------Solutions------------- what is the differnce Serializable objec

  • How do I access class variables of a parent object in PHP? 2008-09-15

    An instance of class A instanciates a couple of other objects, say for example from class B: $foo = new B(); I would like to access A's public class variables from methods within B. Unless I'm missing something, the only way to do this is to pass the

  • C# - Optimising binary serialization for multi-dimensional generic arrays 2008-10-21

    I have a class that I need to binary serialize. The class contains one field as below: private T[,] m_data; These multi-dimensional arrays can be fairly large (hundreds of thousands of elements) and of any primitive type. When I tried standard .net s

  • .NET binary serialization between 32-bit and 64-bit OS 2009-05-28

    I have a program that saves data to hard drive using binary serialization. If I recompile the program on a 64-bit machine, can it deserialize the data from 32-bit machine? --------------Solutions------------- Almost all .NET data types are defined in

  • Silverlight 3.0 Binary Serialization Support? 2009-08-17

    Can I deserialize an object in the Silverlight 3.0 runtime that was serialized using the full .NET 2.0 runtime using the BinaryFormatter? I am using the following code to serialize an object to a ByteArray which we write to a DB table: MemoryStream s

  • Good way to get size of class for binary serialization 2009-12-10

    I'm writing some classes that are going to be used to communicate with a C# TCP server, and I'm writing serialization/deserialization methods using BinaryWriter and BinaryReader. This is easy enough, but I've run into a situation where I have a class

  • Ignore binary serialization on a property 2010-02-25

    I have a regular C# POCO. At the class level, I am decorating the object with [Serializable()]. That said, I am using the Linq Sum() on one of the properties and I am receiving an error upon serialization. If possible, I would like to just simply ign

  • protobuf-net NOT faster than binary serialization? 2010-06-03

    I wrote a program to serialize a 'Person' class using XMLSerializer, BinaryFormatter and ProtoBuf. I thought protobuf-net should be faster than the other two. Protobuf serialization was faster than XMLSerialization but much slower than the binary ser

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 1.489 (s). 13 q(s)