By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Serilization problems

P: n/a
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.SerializationExceptio n: 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)
}
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stephan Schlicker" <St**************@discussions.microsoft.com> wrote in
message news:7C**********************************@microsof t.com...
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.SerializationExceptio n: 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)
}

Nov 16 '05 #2

P: n/a
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(Stre amingContextStates.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!
Nov 16 '05 #3

P: n/a
> >
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!


Hi Stephan,

This is from your code:
[NonSerialized]
private static CDBInterface cDBInterface = new CDBInterface();
I think this is what Nicholas was talking about.

--- Nick
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.