Problem to serialize a interface in Xml

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pol Bawin

    #1

    Problem to serialize a interface in Xml


    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;}
    }
    }


  • Jeffrey Tan[MSFT]

    #2
    RE: Problem to serialize a interface in Xml


    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" <pb@rtech.be>
    | 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: <#R0ChoOiDHA.20 04@TK2MSFTNGP12 .phx.gbl>
    | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
    | NNTP-Posting-Host: 194.78.114.66
    | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1885 49
    | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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;}
    | }
    | }
    |
    |
    |

    Comment

    • Pol Bawin

      #3
      Re: Problem to serialize a interface in Xml

      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]" <v-jetan@online.mi crosoft.com> a écrit dans le message
      de news: QbuSgDYiDHA.192 8@cpmsftngxa06. phx.gbl...[color=blue]
      >
      > 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[/color]
      to[color=blue]
      > 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" <pb@rtech.be>
      > | 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: <#R0ChoOiDHA.20 04@TK2MSFTNGP12 .phx.gbl>
      > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      > | NNTP-Posting-Host: 194.78.114.66
      > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
      > | Xref: cpmsftngxa06.ph x.gbl[/color]
      microsoft.publi c.dotnet.langua ges.csharp:1885 49[color=blue]
      > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      > |
      > |
      > | A class has a private field of type IWizard (An interface) and a[/color]
      public[color=blue]
      > | property to access it.
      > | When I try to serialize the Geometry class in XML, i have an error but[/color]
      it[color=blue]
      > | works in Binary
      > |
      > | Can you help me
      > |
      > | [Serializable]
      > | public class Geometry
      > | {
      > | ....
      > | private IWizard _wizard;
      > | public IWizard Wizard
      > | {
      > | get { return _wizard ;}
      > | set { _wizard = value;}
      > | }
      > | }
      > |
      > |
      > |
      >[/color]


      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: Problem to serialize a interface in Xml


        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" <pb@rtech.be>
        | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | References: <#R0ChoOiDHA.20 04@TK2MSFTNGP12 .phx.gbl>
        <QbuSgDYiDHA.19 28@cpmsftngxa06 .phx.gbl>
        | 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: <3f7d9053$0$106 95$ba620e4c@rea der2.news.skyne t.be>
        | Organization: -= Belgacom Usenet Service =-
        | NNTP-Posting-Host: 194.78.114.66
        | X-Trace: 1065193555 reader2.news.sk ynet.be 10695 194.78.114.66:4 5476
        | X-Complaints-To: usenet-abuse@skynet.be
        | Path:
        cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-onlin
        e.de!skynet.be! skynet.be!louie !tlk!not-for-mail
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1887 72
        | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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]" <v-jetan@online.mi crosoft.com> a écrit dans le message
        | de news: QbuSgDYiDHA.192 8@cpmsftngxa06. phx.gbl...
        | >
        | > 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" <pb@rtech.be>
        | > | 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: <#R0ChoOiDHA.20 04@TK2MSFTNGP12 .phx.gbl>
        | > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | > | NNTP-Posting-Host: 194.78.114.66
        | > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
        | > | Xref: cpmsftngxa06.ph x.gbl
        | microsoft.publi c.dotnet.langua ges.csharp:1885 49
        | > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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;}
        | > | }
        | > | }
        | > |
        | > |
        | > |
        | >
        |
        |
        |

        Comment

        Working...