Serilization problems

G

Guest

Hi everyone

I have a serious problem with the serilization of an user-defined object in
C#. I would like to serialize an object of class D which inherits from class
C. Class C as well as class D are marked with the compiler-attribute
[Serializable] (see also code-snippet at the end of the message).
It is no problem to serialize a instance of class C, but I got the following
error-message if I try to serialize a instance of class D:

An unhandled exception of type
System.Runtime.Serialization.SerializationException: The type <Namespace>.B
in Assembly <AssemblyName>, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null is not marked as serializes.

The problem I have is, that the variable which causes the Exception is a
member of the base-class C which can be serialized without any problems.
In addition to that, the variable is also marked with the
[NonSerialized]-attribute, so the value of this variable should not be
serialized (see also in the code-snippet).

Thanks for any help!

greetings stephan


Code:

public class A : MarshalByRefObject, IDisposable
{
//some ints, Hashtables, IPAddresses (all
//Serializable)

protected A a
}


public class B : A
{
protected new B a
}


[Serializable]
public class C : MarshalByRefObject, IDisposable
{
[NonSerialized]
protected B a;

//some Guids and longs (all serializable)
}


[Serializable]
public class D: C
{
[NonSerialized]
private static CDBInterface cDBInterface = new CDBInterface();

//some Hashtables (all serializable)
}
 
N

Nicholas Paldino [.NET/C# MVP]

Stephan,

Using the code that you provided, I can serialize and deserialize the
instance of D no problem.

I do not think that you should "new" any of the members of the classes,
as you will have to make sure to account for both the value on the base
class and the derived class.

Can you post an example that shows the problem?

Also, you don't have to mark static members as non-serializable, as they
are not eligibile for serialization.
 
S

Stephan Schlicker

Hi Nicolas
Using the code that you provided, I can serialize and deserialize the
instance of D no problem.

I do not think that you should "new" any of the members of the classes,
as you will have to make sure to account for both the value on the base
class and the derived class.
Yes, I now that problem! But at the moment this solution works just fine.
Can you post an example that shows the problem?
I'll try to give you an example. I'm sorry that I can not post the whole
code because it's code for my master thesis and this code is also supposed
to build the base of some non-open-source software!

[Serializable]
public class C : MarshalByRefObject, IDisposable
{
[NonSerialized]
protected B framework;
protected Guid systemID;
protected long userID;

public C(long _userID, B _framework)
{
this.userID = _userID;
this.framework = _framework;
this.RegisterAtFramework();
}
}

[Serializable]
public class D: C
{
[NonSerialized]
private static CDBInterface cDBInterface = new CDBInterface();
[NonSerialized]
private Hashtable x = new Hashtable();
[NonSerialized]
private Hashtable y = new Hashtable();
[NonSerialized]
private Hashtable z = new Hashtable();


public CustomerManager(long userID, SimMarketAgentFramework
framework) : base(userID, framework)
{
ArrayList d = new ArrayList();
foreach(C c in a.Values)
d.Add(c);
this.framework.Register(this.framework.FrameworkID, d,
this.userID);
}
}

Those are all Members of both classes and their corresponding Constructor
which I use!
After all objects are created I manually call the following method for one
speciall instance of D:

public void Send(IPAddress address, Guid iD)
{
C c = (c)this.cReferences[ID];
B framework = (B)Activator.GetObject(typeof(B), "http://" + address
+ ":1234/B.soap");

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Context = new
System.Runtime.Serialization.StreamingContext(StreamingContextStates.Clone);
formatter.Serialize(stream, c);

bool received = framework.Receive(stream);

if(received)
{
c.Dispose();
this.cReferences.Remove(agentID);
}
}

And the last statement which is executed correctly is
formatter.Context(...)! The statement formatter.Serialize(stream, c) crashes
and produces that mentioned exception!
Also, you don't have to mark static members as non-serializable, as they
are not eligibile for serialization.
I don't have any static members at the moment!
 
N

Nick Malik

I don't have any static members at the moment!

Hi Stephan,

This is from your code:
[NonSerialized]
private static CDBInterface cDBInterface = new CDBInterface();


I think this is what Nicholas was talking about.

--- Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top