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

Serializing an object that has members of type interface

P: n/a
axr
Having trouble with Serilization of objects that contain members which are of
type Interface

eg
public class SomeClass
{

ISomeInterface1 itf1;
ClassType1 ct1;
ISomeInterface2 itf2;
/**
// code
**/
}
itf1 and itf2 are instances of classes that implement the respective
interfaces.
The ClassType in turn may have other members that are of type interfaces.
While serializing this barfs with error "Cannot serialize xx because its an
interface."
Any pointers on how to serialize this kind of an object?? (without getting
rid of the interface members)
Nov 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Did you ever get a reply to this question?

"axr" wrote:
Having trouble with Serilization of objects that contain members which are of
type Interface

eg
public class SomeClass
{

ISomeInterface1 itf1;
ClassType1 ct1;
ISomeInterface2 itf2;
/**
// code
**/
}
itf1 and itf2 are instances of classes that implement the respective
interfaces.
The ClassType in turn may have other members that are of type interfaces.
While serializing this barfs with error "Cannot serialize xx because its an
interface."
Any pointers on how to serialize this kind of an object?? (without getting
rid of the interface members)

Nov 23 '05 #2

P: n/a
DC
An interface collects only methods.
Since the XmlSerializer serializes only public fields and properties, the
XmlSerializer necessarily does not "do" interfaces.

Suppose your custom type has a public property that is ISomeInterface, if
the serializer peeked into that interface it would find no public fields or
methods; essentially there is no data attached to the interface. Therefore
it makes sense for nothing to be serialized there.

By serializing interfaces, what are you hoping to accomplish? what is your
goal?

If you want to serialize the state of an object, consider the binary
serializer.

-D
"Jack" <Ja**@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
Did you ever get a reply to this question?

"axr" wrote:
Having trouble with Serilization of objects that contain members which
are of
type Interface

eg
public class SomeClass
{

ISomeInterface1 itf1;
ClassType1 ct1;
ISomeInterface2 itf2;
/**
// code
**/
}
itf1 and itf2 are instances of classes that implement the respective
interfaces.
The ClassType in turn may have other members that are of type interfaces.
While serializing this barfs with error "Cannot serialize xx because its
an
interface."
Any pointers on how to serialize this kind of an object?? (without
getting
rid of the interface members)

Nov 23 '05 #3

P: n/a
"DC" wrote:
An interface collects only methods.
Since the XmlSerializer serializes only public fields and properties, the
XmlSerializer necessarily does not "do" interfaces.

Suppose your custom type has a public property that is ISomeInterface, if
the serializer peeked into that interface it would find no public fields or
methods; essentially there is no data attached to the interface. Therefore
it makes sense for nothing to be serialized there.

By serializing interfaces, what are you hoping to accomplish? what is your
goal?


My IFoo interface, that contains properties, is implemented by a series of
classes, FooImpl1 and FooImpl2. I've created a collection class FooCollection
(implementing ICollection & more) that contains IFoo objects. I want to
serialize the objects in the collection.

My workaround is to convert IFoo into an abstract/viirtual base class, Foo
which is then extended by FooImpl1 and FooImpl2, however, this doesn't work
when a class needs to implement multiple interfaces.
Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.