Serialize and deserialize against interface??

G

Guest

oop Q:

If I serialize an interface array, then later deserialize it, do I need the
original
assembly to deserialize it???

Eg:
// Inside of "globaly assembly"
public interface Ianimal
{
string myColor
{ get;}
}

// "Dog Assembly" Inside of dynamicaly loaded assembly.
// Knows only "global Assembly"
public class dog: Ianimal
{
string myColor
{
get{return "black";}
}
}

// my main Assembly where the "magic" happens.
//Initialy only knows the "global assembly"
pubilc class myMainClass
{
Type AnimalType;

void DynamiclyLoadAssembly(string assemblyFileName)
{ //ommitted,but this is where AnimalType is defined}

private Ianimal[] myAnimals;

void SomeFunction(int x)
{
Ianimal[x] = (Ianimal) (new AnimalType());
}

private SerializeAnimals(Ianimals[] myAnimals )
{//ommitted}

private DeserializeMyAnimals()
{
//Q: If the next time I run the app, and I dont load the "Dog Assembly",
// will this fail?????????????????????????????
}
}


Thanks in advance.
 
D

Dave Sexton

Hi,

No, that will not work. The underlying Type is required, and if the
formatter cannot find it then it will fail.

An interface requires an implementation and so cannot be constructed - it's
the concrete type that must be constructed. The full type name of the
concrete implementation that was serialized is stored in the serialized
object graph and the formatter will attempt to use this information to load
the Type meta data at runtime. Obviously, if your assembly is not
referencing the type's assembly, then the formatter will not be able to
resolve the Type and the operation will fail.
 
D

DeveloperX

I've seen this confusion before with remoting and serialization. When
you remote a Server Activated Object you don't need access to the
concrete implementation any interface implemented on the object will
suffice and in fact is the prefered way of going about it. So some
people make a leap of faith and assume that this will work with
serialization as well.

No idea if that's where you were coming from, but thought it added a
tincy tiny bit of value :)
 

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