Serialization and inheritance

G

Gerben

Hello,

I'm getting an error when I try to serialize a class in my project. Hope
someone can help me out:

Shared assembly:

[Serializable]
public abstract class Person
{
public int PersonData;
}

Project1

[Serializable]
public class Student : Person
{

}

Person person1 = new Student();

Project2:

I like to serialize person1 (using .net remoting) so I can use that object
in this project. Project2 only knows the person class, but that is enough
since I only need to access PersonData and nothing else. When I try this i'm
getting a serializable exception that says that project2 cannot find
Project1. But I don't want to include project1 or the student class. Is
there a way to solve this / can I tell Student to only serialize the Person
data in some way?

greets,

Gerben.
 
N

Nicholas Paldino [.NET/C# MVP]

Gerben,

Unfortunately, no, you can not. You need to have a reference to the
assembly that contains the type you are going to serialize so that the
serialization framework will be able to load the appropriate type/field data
for serialization.

Hope this helps.
 
J

Jeff Louie

Gerben... Net1.1 serialization was version specific. Net2.0 suppports
backward compatibility of versioning if you declare the new methods as
optional. I cannot find a reference to this online right now, but this
approach could be helpful.

Regards,
Jeff
 
G

Gerben

That's a pity. Forces me to move my student class to the shared assembly, it
isn't neat, but it works.

Thanks for your fast reply,

Gerben.

Nicholas Paldino said:
Gerben,

Unfortunately, no, you can not. You need to have a reference to the
assembly that contains the type you are going to serialize so that the
serialization framework will be able to load the appropriate type/field
data for serialization.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Gerben said:
Hello,

I'm getting an error when I try to serialize a class in my project. Hope
someone can help me out:

Shared assembly:

[Serializable]
public abstract class Person
{
public int PersonData;
}

Project1

[Serializable]
public class Student : Person
{

}

Person person1 = new Student();

Project2:

I like to serialize person1 (using .net remoting) so I can use that
object in this project. Project2 only knows the person class, but that is
enough since I only need to access PersonData and nothing else. When I
try this i'm getting a serializable exception that says that project2
cannot find Project1. But I don't want to include project1 or the student
class. Is there a way to solve this / can I tell Student to only
serialize the Person data in some way?

greets,

Gerben.
 

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