Problem to serialize a interface in Xml

  • Thread starter Thread starter Pol Bawin
  • Start date Start date
P

Pol Bawin

A class has a private field of type IWizard (An interface) and a public
property to access it.
When I try to serialize the Geometry class in XML, i have an error but it
works in Binary

Can you help me

[Serializable]
public class Geometry
{
....
private IWizard _wizard;
public IWizard Wizard
{
get { return _wizard ;}
set { _wizard = value;}
}
}
 
Hi Pol,

I think that this has something to do with the one of the follwoing:
1. the XmlSerializer has to able able to instantiate an object during
deserialization. For an interface, however, it would not know what type to
instantiate.

2. serializing an interface only may only serialize the part of the class
that implements the interface, but not public fields that are not part of
the interface. When deserializing the instance would not have all it's
public fields in the state it was in when you passed the interface to the
Serialize method.

To work around this problem, you can You can use an abstract base class to
express the semantics of your interface.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Pol Bawin" <[email protected]>
| Subject: Problem to serialize a interface in Xml
| Date: Thu, 2 Oct 2003 15:32:35 +0200
| Lines: 21
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 194.78.114.66
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:188549
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
|
| A class has a private field of type IWizard (An interface) and a public
| property to access it.
| When I try to serialize the Geometry class in XML, i have an error but it
| works in Binary
|
| Can you help me
|
| [Serializable]
| public class Geometry
| {
| ....
| private IWizard _wizard;
| public IWizard Wizard
| {
| get { return _wizard ;}
| set { _wizard = value;}
| }
| }
|
|
|
 
Ok but in this case, an attribute (xmlElement(...) or xmlType(...) ) must be
added. Ok for a static structure but I my case these Type can be added
dynamicly.
 
Hi Pol,

Thanks for your reply.
Can you tell me what does your " these Type can be added dynamicly" mean?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Pol Bawin" <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| References: <#[email protected]>
<[email protected]>
| Subject: Re: Problem to serialize a interface in Xml
| Date: Fri, 3 Oct 2003 17:05:55 +0200
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Lines: 76
| Message-ID: <[email protected]>
| Organization: -= Belgacom Usenet Service =-
| NNTP-Posting-Host: 194.78.114.66
| X-Trace: 1065193555 reader2.news.skynet.be 10695 194.78.114.66:45476
| X-Complaints-To: (e-mail address removed)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!skynet.be!skynet.be!louie!tlk!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:188772
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Ok but in this case, an attribute (xmlElement(...) or xmlType(...) ) must
be
| added. Ok for a static structure but I my case these Type can be added
| dynamicly.
|
| "Jeffrey Tan[MSFT]" <[email protected]> a écrit dans le message
| de news: (e-mail address removed)...
| >
| > Hi Pol,
| >
| > I think that this has something to do with the one of the follwoing:
| > 1. the XmlSerializer has to able able to instantiate an object during
| > deserialization. For an interface, however, it would not know what type
to
| > instantiate.
| >
| > 2. serializing an interface only may only serialize the part of the
class
| > that implements the interface, but not public fields that are not part
of
| > the interface. When deserializing the instance would not have all it's
| > public fields in the state it was in when you passed the interface to
the
| > Serialize method.
| >
| > To work around this problem, you can You can use an abstract base class
| to
| > express the semantics of your interface.
| >
| > Hope this helps,
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Pol Bawin" <[email protected]>
| > | Subject: Problem to serialize a interface in Xml
| > | Date: Thu, 2 Oct 2003 15:32:35 +0200
| > | Lines: 21
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 194.78.114.66
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:188549
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > |
| > | A class has a private field of type IWizard (An interface) and a
| public
| > | property to access it.
| > | When I try to serialize the Geometry class in XML, i have an error but
| it
| > | works in Binary
| > |
| > | Can you help me
| > |
| > | [Serializable]
| > | public class Geometry
| > | {
| > | ....
| > | private IWizard _wizard;
| > | public IWizard Wizard
| > | {
| > | get { return _wizard ;}
| > | set { _wizard = value;}
| > | }
| > | }
| > |
| > |
| > |
| >
|
|
|
 
Back
Top