Serialization and ISite

M

mijalko

Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?
 
N

Nicholas Paldino [.NET/C# MVP]

If you implement the ISerializable interface, this is not going to give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on properties
doesn't matter, since the serialization engine is always going to dig into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that your
components site is either serializable, or don't serialize the reference to
the site in your custom ISerializable implementation.

Hope this helps.
 
M

mijalko

or don't serialize the reference to
the site in your custom ISerializable implementation.

Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{

public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
....
}

....
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
....


That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.




If you implement the ISerializable interface, this is not going to give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on properties
doesn't matter, since the serialization engine is always going to dig into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that your
components site is either serializable, or don't serialize the reference to
the site in your custom ISerializable implementation.

Hope this helps.

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




Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted text -

- Show quoted text -
 
N

Nicholas Paldino [.NET/C# MVP]

Well, that's the problem. Why are you using the XmlSerializer class
when the ISerializable interface is for Binary and Soap serialization?

If you want plain old xml for serialization, you need to implement the
IXmlSerializable interface, as THAT will give you control over the
serialization details.

If you are ok with Binary/SOAP serialization, then you can use the
BinaryFormatter or SoapFormatter classes to drive the serialization in the
System.Runtime.Serialization.Formatters.Binary and
System.Runtime.Serialization.Formatters.Soap namespaces, respectively.


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

or don't serialize the reference to
the site in your custom ISerializable implementation.

Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{

public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
...
}

...
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
...


That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.




If you implement the ISerializable interface, this is not going to
give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties
exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the
object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on
properties
doesn't matter, since the serialization engine is always going to dig
into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that
your
components site is either serializable, or don't serialize the reference
to
the site in your custom ISerializable implementation.

Hope this helps.

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




Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted
text -

- Show quoted text -
 
M

mijalko

It's clear now.
Thanks a lot

Well, that's the problem. Why are you using the XmlSerializer class
when the ISerializable interface is for Binary and Soap serialization?

If you want plain old xml for serialization, you need to implement the
IXmlSerializable interface, as THAT will give you control over the
serialization details.

If you are ok with Binary/SOAP serialization, then you can use the
BinaryFormatter or SoapFormatter classes to drive the serialization in the
System.Runtime.Serialization.Formatters.Binary and
System.Runtime.Serialization.Formatters.Soap namespaces, respectively.

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






Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
...
}
...
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
...
That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.
If you implement the ISerializable interface, this is not going to
give
you the same semantics as using IXmlSerializer.
When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties
exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the
object.
When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on
properties
doesn't matter, since the serialization engine is always going to dig
into
the type and get the fields.
However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.
I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that
your
components site is either serializable, or don't serialize the reference
to
the site in your custom ISerializable implementation.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted
text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 

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