How to prevent base class (UserControl) form being serialized

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

    #1

    How to prevent base class (UserControl) form being serialized

    I have a class that is a control derived from UserControl. I want to use
    serialization and deserialization with this calss but I get an exception
    "Cannot serialize member System.Componen tModel.Componen t.Site of type
    System.Componen tModel.ISite because it is an interface.".

    I am not interested in serializing any member from the base class only the
    properties in the derived class.

    How can I prevent the entire base class from being serialized?


    Code snippet

    public partial class UserControl1 : UserControl
    {
    private int item1;
    private string item2;


    public UserControl1()
    {

    }

    public int Item1
    {
    get {return item1;}
    set {item1 = value;}
    }

    public string Item2
    {
    get { return item2; }
    set { item2 = value; }
    }
    }


    public partial class Form1 : Form
    {
    private UserControl1 Uctrl1;
  • Peter Rilling

    #2
    Re: How to prevent base class (UserControl) form being serialized

    Well, I doubt that you can. There are attributes that will let you
    determine what can and cannot be serialized, but you would have to place
    them on the code for the UserControl.

    Now, let's step back for a moment and talk about design. I imaging you want
    to serialize your control so that you can persist it and then reload it at
    some later time with a complete state. The base UserControl is part of your
    derived control and it would not make sense to reinitialize only your
    derived control while leaving the base class un-touched. My suggestion
    would be to create a data structure that maintain a copy of all the
    information in your control, for instance, if you had name, address, and
    phone number, you then might have the following structure.

    public struct Person{
    Public Name;
    Public Address;
    Public PhoneNumber;
    }

    The design principle here is that you decouple the data from the
    presentation. That way you can do what you want with the data (store it or
    move it as needed) then when you need to reload your control, you simply
    initialize the fields with this data. Just serialize this structure rather
    than worrying about the whole control.


    "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
    news:89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com...[color=blue]
    >I have a class that is a control derived from UserControl. I want to use
    > serialization and deserialization with this calss but I get an exception
    > "Cannot serialize member System.Componen tModel.Componen t.Site of type
    > System.Componen tModel.ISite because it is an interface.".
    >
    > I am not interested in serializing any member from the base class only the
    > properties in the derived class.
    >
    > How can I prevent the entire base class from being serialized?
    >
    >
    > Code snippet
    >
    > public partial class UserControl1 : UserControl
    > {
    > private int item1;
    > private string item2;
    >
    >
    > public UserControl1()
    > {
    >
    > }
    >
    > public int Item1
    > {
    > get {return item1;}
    > set {item1 = value;}
    > }
    >
    > public string Item2
    > {
    > get { return item2; }
    > set { item2 = value; }
    > }
    > }
    >
    >
    > public partial class Form1 : Form
    > {
    > private UserControl1 Uctrl1;
    > .
    > .
    > .
    > Uctrl1 = new UserControl1();
    > Uctrl1.Item1 = 3;
    > Uctrl1.Item2 = "This is a test";
    >
    > .
    > .
    > .
    > // error on line below
    > XmlSerializer mySerializer = new XmlSerializer(t ypeof(UserContr ol1));
    > // To write to a file, create a StreamWriter object.
    > StreamWriter myWriter = new StreamWriter("m yFileName.xml") ;
    > mySerializer.Se rialize(myWrite r, Uctrl1);
    > myWriter.Close( );
    > }
    >
    >
    > Reposted with proper MSDN alias
    > --
    > EqDev[/color]


    Comment

    • EqDev

      #3
      Re: How to prevent base class (UserControl) form being serialized

      Thanks for the reply.

      Ok I get that, but the idea is serializing all public members of an object.
      My object happens to be derived from a control so I am penalizes for that.
      This makes for more work for maintenance of the code for the object. If I
      add public assessors now I must also maintain a struct as well. I was hoping
      that there was some way via attributes to force the serializer to ignore the
      base class.

      --
      EqDev


      "Peter Rilling" wrote:
      [color=blue]
      > Well, I doubt that you can. There are attributes that will let you
      > determine what can and cannot be serialized, but you would have to place
      > them on the code for the UserControl.
      >
      > Now, let's step back for a moment and talk about design. I imaging you want
      > to serialize your control so that you can persist it and then reload it at
      > some later time with a complete state. The base UserControl is part of your
      > derived control and it would not make sense to reinitialize only your
      > derived control while leaving the base class un-touched. My suggestion
      > would be to create a data structure that maintain a copy of all the
      > information in your control, for instance, if you had name, address, and
      > phone number, you then might have the following structure.
      >
      > public struct Person{
      > Public Name;
      > Public Address;
      > Public PhoneNumber;
      > }
      >
      > The design principle here is that you decouple the data from the
      > presentation. That way you can do what you want with the data (store it or
      > move it as needed) then when you need to reload your control, you simply
      > initialize the fields with this data. Just serialize this structure rather
      > than worrying about the whole control.
      >
      >
      > "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
      > news:89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com...[color=green]
      > >I have a class that is a control derived from UserControl. I want to use
      > > serialization and deserialization with this calss but I get an exception
      > > "Cannot serialize member System.Componen tModel.Componen t.Site of type
      > > System.Componen tModel.ISite because it is an interface.".
      > >
      > > I am not interested in serializing any member from the base class only the
      > > properties in the derived class.
      > >
      > > How can I prevent the entire base class from being serialized?
      > >
      > >
      > > Code snippet
      > >
      > > public partial class UserControl1 : UserControl
      > > {
      > > private int item1;
      > > private string item2;
      > >
      > >
      > > public UserControl1()
      > > {
      > >
      > > }
      > >
      > > public int Item1
      > > {
      > > get {return item1;}
      > > set {item1 = value;}
      > > }
      > >
      > > public string Item2
      > > {
      > > get { return item2; }
      > > set { item2 = value; }
      > > }
      > > }
      > >
      > >
      > > public partial class Form1 : Form
      > > {
      > > private UserControl1 Uctrl1;
      > > .
      > > .
      > > .
      > > Uctrl1 = new UserControl1();
      > > Uctrl1.Item1 = 3;
      > > Uctrl1.Item2 = "This is a test";
      > >
      > > .
      > > .
      > > .
      > > // error on line below
      > > XmlSerializer mySerializer = new XmlSerializer(t ypeof(UserContr ol1));
      > > // To write to a file, create a StreamWriter object.
      > > StreamWriter myWriter = new StreamWriter("m yFileName.xml") ;
      > > mySerializer.Se rialize(myWrite r, Uctrl1);
      > > myWriter.Close( );
      > > }
      > >
      > >
      > > Reposted with proper MSDN alias
      > > --
      > > EqDev[/color]
      >
      >
      >[/color]

      Comment

      • Peter Rilling

        #4
        Re: How to prevent base class (UserControl) form being serialized

        If it was possible, what happens when you de-serialize? It is also ignore
        the base class so your control won't have a name, know its visibility, know
        what parent container owns it, location, etc.

        "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
        news:1323810C-E41B-4E20-A766-EAA726BC8354@mi crosoft.com...[color=blue]
        > Thanks for the reply.
        >
        > Ok I get that, but the idea is serializing all public members of an
        > object.
        > My object happens to be derived from a control so I am penalizes for that.
        > This makes for more work for maintenance of the code for the object. If I
        > add public assessors now I must also maintain a struct as well. I was
        > hoping
        > that there was some way via attributes to force the serializer to ignore
        > the
        > base class.
        >
        > --
        > EqDev
        >
        >
        > "Peter Rilling" wrote:
        >[color=green]
        >> Well, I doubt that you can. There are attributes that will let you
        >> determine what can and cannot be serialized, but you would have to place
        >> them on the code for the UserControl.
        >>
        >> Now, let's step back for a moment and talk about design. I imaging you
        >> want
        >> to serialize your control so that you can persist it and then reload it
        >> at
        >> some later time with a complete state. The base UserControl is part of
        >> your
        >> derived control and it would not make sense to reinitialize only your
        >> derived control while leaving the base class un-touched. My suggestion
        >> would be to create a data structure that maintain a copy of all the
        >> information in your control, for instance, if you had name, address, and
        >> phone number, you then might have the following structure.
        >>
        >> public struct Person{
        >> Public Name;
        >> Public Address;
        >> Public PhoneNumber;
        >> }
        >>
        >> The design principle here is that you decouple the data from the
        >> presentation. That way you can do what you want with the data (store it
        >> or
        >> move it as needed) then when you need to reload your control, you simply
        >> initialize the fields with this data. Just serialize this structure
        >> rather
        >> than worrying about the whole control.
        >>
        >>
        >> "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
        >> news:89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com...[color=darkred]
        >> >I have a class that is a control derived from UserControl. I want to
        >> >use
        >> > serialization and deserialization with this calss but I get an
        >> > exception
        >> > "Cannot serialize member System.Componen tModel.Componen t.Site of type
        >> > System.Componen tModel.ISite because it is an interface.".
        >> >
        >> > I am not interested in serializing any member from the base class only
        >> > the
        >> > properties in the derived class.
        >> >
        >> > How can I prevent the entire base class from being serialized?
        >> >
        >> >
        >> > Code snippet
        >> >
        >> > public partial class UserControl1 : UserControl
        >> > {
        >> > private int item1;
        >> > private string item2;
        >> >
        >> >
        >> > public UserControl1()
        >> > {
        >> >
        >> > }
        >> >
        >> > public int Item1
        >> > {
        >> > get {return item1;}
        >> > set {item1 = value;}
        >> > }
        >> >
        >> > public string Item2
        >> > {
        >> > get { return item2; }
        >> > set { item2 = value; }
        >> > }
        >> > }
        >> >
        >> >
        >> > public partial class Form1 : Form
        >> > {
        >> > private UserControl1 Uctrl1;
        >> > .
        >> > .
        >> > .
        >> > Uctrl1 = new UserControl1();
        >> > Uctrl1.Item1 = 3;
        >> > Uctrl1.Item2 = "This is a test";
        >> >
        >> > .
        >> > .
        >> > .
        >> > // error on line below
        >> > XmlSerializer mySerializer = new
        >> > XmlSerializer(t ypeof(UserContr ol1));
        >> > // To write to a file, create a StreamWriter object.
        >> > StreamWriter myWriter = new StreamWriter("m yFileName.xml") ;
        >> > mySerializer.Se rialize(myWrite r, Uctrl1);
        >> > myWriter.Close( );
        >> > }
        >> >
        >> >
        >> > Reposted with proper MSDN alias
        >> > --
        >> > EqDev[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • EqDev

          #5
          Re: How to prevent base class (UserControl) form being serialized

          Yes, but in the ideal situation you would have control over what is and is
          not serialized.

          Does anyone from MS have a suggestion on this?
          Is there a way in .NET 2.0 to deal with this issue?

          --
          EqDev


          "Peter Rilling" wrote:
          [color=blue]
          > If it was possible, what happens when you de-serialize? It is also ignore
          > the base class so your control won't have a name, know its visibility, know
          > what parent container owns it, location, etc.
          >
          > "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
          > news:1323810C-E41B-4E20-A766-EAA726BC8354@mi crosoft.com...[color=green]
          > > Thanks for the reply.
          > >
          > > Ok I get that, but the idea is serializing all public members of an
          > > object.
          > > My object happens to be derived from a control so I am penalizes for that.
          > > This makes for more work for maintenance of the code for the object. If I
          > > add public assessors now I must also maintain a struct as well. I was
          > > hoping
          > > that there was some way via attributes to force the serializer to ignore
          > > the
          > > base class.
          > >
          > > --
          > > EqDev
          > >
          > >
          > > "Peter Rilling" wrote:
          > >[color=darkred]
          > >> Well, I doubt that you can. There are attributes that will let you
          > >> determine what can and cannot be serialized, but you would have to place
          > >> them on the code for the UserControl.
          > >>
          > >> Now, let's step back for a moment and talk about design. I imaging you
          > >> want
          > >> to serialize your control so that you can persist it and then reload it
          > >> at
          > >> some later time with a complete state. The base UserControl is part of
          > >> your
          > >> derived control and it would not make sense to reinitialize only your
          > >> derived control while leaving the base class un-touched. My suggestion
          > >> would be to create a data structure that maintain a copy of all the
          > >> information in your control, for instance, if you had name, address, and
          > >> phone number, you then might have the following structure.
          > >>
          > >> public struct Person{
          > >> Public Name;
          > >> Public Address;
          > >> Public PhoneNumber;
          > >> }
          > >>
          > >> The design principle here is that you decouple the data from the
          > >> presentation. That way you can do what you want with the data (store it
          > >> or
          > >> move it as needed) then when you need to reload your control, you simply
          > >> initialize the fields with this data. Just serialize this structure
          > >> rather
          > >> than worrying about the whole control.
          > >>
          > >>
          > >> "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
          > >> news:89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com...
          > >> >I have a class that is a control derived from UserControl. I want to
          > >> >use
          > >> > serialization and deserialization with this calss but I get an
          > >> > exception
          > >> > "Cannot serialize member System.Componen tModel.Componen t.Site of type
          > >> > System.Componen tModel.ISite because it is an interface.".
          > >> >
          > >> > I am not interested in serializing any member from the base class only
          > >> > the
          > >> > properties in the derived class.
          > >> >
          > >> > How can I prevent the entire base class from being serialized?
          > >> >
          > >> >
          > >> > Code snippet
          > >> >
          > >> > public partial class UserControl1 : UserControl
          > >> > {
          > >> > private int item1;
          > >> > private string item2;
          > >> >
          > >> >
          > >> > public UserControl1()
          > >> > {
          > >> >
          > >> > }
          > >> >
          > >> > public int Item1
          > >> > {
          > >> > get {return item1;}
          > >> > set {item1 = value;}
          > >> > }
          > >> >
          > >> > public string Item2
          > >> > {
          > >> > get { return item2; }
          > >> > set { item2 = value; }
          > >> > }
          > >> > }
          > >> >
          > >> >
          > >> > public partial class Form1 : Form
          > >> > {
          > >> > private UserControl1 Uctrl1;
          > >> > .
          > >> > .
          > >> > .
          > >> > Uctrl1 = new UserControl1();
          > >> > Uctrl1.Item1 = 3;
          > >> > Uctrl1.Item2 = "This is a test";
          > >> >
          > >> > .
          > >> > .
          > >> > .
          > >> > // error on line below
          > >> > XmlSerializer mySerializer = new
          > >> > XmlSerializer(t ypeof(UserContr ol1));
          > >> > // To write to a file, create a StreamWriter object.
          > >> > StreamWriter myWriter = new StreamWriter("m yFileName.xml") ;
          > >> > mySerializer.Se rialize(myWrite r, Uctrl1);
          > >> > myWriter.Close( );
          > >> > }
          > >> >
          > >> >
          > >> > Reposted with proper MSDN alias
          > >> > --
          > >> > EqDev
          > >>
          > >>
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • TerryFei

            #6
            Re: How to prevent base class (UserControl) form being serialized

            Hi EqDev,
            Welcome to MSDN Newsgroup!

            Based on object model, when a class derived from other class, base class's
            content will be involved in child class. So there is currently no way to do
            this, but I will also forward this issue to the relevant team. If there is
            any information later, I will inform of you as soon as possible. Thanks for
            your understanding. Have a nice day!

            Best Regards,

            Terry Fei [MSFT]
            Microsoft Community Support
            Get Secure! www.microsoft.com/security

            --------------------[color=blue]
            >Thread-Topic: How to prevent base class (UserControl) form being serialized
            >thread-index: AcYr357YOeoId7s FR96a2R1fkmd/+g==
            >X-WBNR-Posting-Host: 24.249.212.34
            >From: "=?Utf-8?B?RXFEZXY=?=" <eqdev@newsgrou ps.nospam>
            >References: <89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com>[/color]
            <umO37I0KGHA.23 36@TK2MSFTNGP12 .phx.gbl>
            <1323810C-E41B-4E20-A766-EAA726BC8354@mi crosoft.com>
            <OCD9Li2KGHA.27 04@TK2MSFTNGP15 .phx.gbl>[color=blue]
            >Subject: Re: How to prevent base class (UserControl) form being serialized
            >Date: Tue, 7 Feb 2006 04:11:27 -0800
            >Lines: 142
            >Message-ID: <52C5884F-A758-4911-9DFD-8BD60BA434CE@mi crosoft.com>
            >MIME-Version: 1.0
            >Content-Type: text/plain;
            > charset="Utf-8"
            >Content-Transfer-Encoding: 7bit
            >X-Newsreader: Microsoft CDO for Windows 2000
            >Content-Class: urn:content-classes:message
            >Importance: normal
            >Priority: normal
            >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
            >Newsgroups: microsoft.publi c.dotnet.genera l
            >NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
            >Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GXA03.phx.gbl
            >Xref: TK2MSFTNGXA02.p hx.gbl microsoft.publi c.dotnet.genera l:188179
            >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
            >
            >Yes, but in the ideal situation you would have control over what is and is
            >not serialized.
            >
            >Does anyone from MS have a suggestion on this?
            >Is there a way in .NET 2.0 to deal with this issue?
            >
            >--
            >EqDev
            >
            >
            >"Peter Rilling" wrote:
            >[color=green]
            >> If it was possible, what happens when you de-serialize? It is also[/color][/color]
            ignore[color=blue][color=green]
            >> the base class so your control won't have a name, know its visibility,[/color][/color]
            know[color=blue][color=green]
            >> what parent container owns it, location, etc.
            >>
            >> "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
            >> news:1323810C-E41B-4E20-A766-EAA726BC8354@mi crosoft.com...[color=darkred]
            >> > Thanks for the reply.
            >> >
            >> > Ok I get that, but the idea is serializing all public members of an
            >> > object.
            >> > My object happens to be derived from a control so I am penalizes for[/color][/color][/color]
            that.[color=blue][color=green][color=darkred]
            >> > This makes for more work for maintenance of the code for the object.[/color][/color][/color]
            If I[color=blue][color=green][color=darkred]
            >> > add public assessors now I must also maintain a struct as well. I was
            >> > hoping
            >> > that there was some way via attributes to force the serializer to[/color][/color][/color]
            ignore[color=blue][color=green][color=darkred]
            >> > the
            >> > base class.
            >> >
            >> > --
            >> > EqDev
            >> >
            >> >
            >> > "Peter Rilling" wrote:
            >> >
            >> >> Well, I doubt that you can. There are attributes that will let you
            >> >> determine what can and cannot be serialized, but you would have to[/color][/color][/color]
            place[color=blue][color=green][color=darkred]
            >> >> them on the code for the UserControl.
            >> >>
            >> >> Now, let's step back for a moment and talk about design. I imaging[/color][/color][/color]
            you[color=blue][color=green][color=darkred]
            >> >> want
            >> >> to serialize your control so that you can persist it and then reload[/color][/color][/color]
            it[color=blue][color=green][color=darkred]
            >> >> at
            >> >> some later time with a complete state. The base UserControl is part[/color][/color][/color]
            of[color=blue][color=green][color=darkred]
            >> >> your
            >> >> derived control and it would not make sense to reinitialize only your
            >> >> derived control while leaving the base class un-touched. My[/color][/color][/color]
            suggestion[color=blue][color=green][color=darkred]
            >> >> would be to create a data structure that maintain a copy of all the
            >> >> information in your control, for instance, if you had name, address,[/color][/color][/color]
            and[color=blue][color=green][color=darkred]
            >> >> phone number, you then might have the following structure.
            >> >>
            >> >> public struct Person{
            >> >> Public Name;
            >> >> Public Address;
            >> >> Public PhoneNumber;
            >> >> }
            >> >>
            >> >> The design principle here is that you decouple the data from the
            >> >> presentation. That way you can do what you want with the data (store[/color][/color][/color]
            it[color=blue][color=green][color=darkred]
            >> >> or
            >> >> move it as needed) then when you need to reload your control, you[/color][/color][/color]
            simply[color=blue][color=green][color=darkred]
            >> >> initialize the fields with this data. Just serialize this structure
            >> >> rather
            >> >> than worrying about the whole control.
            >> >>
            >> >>
            >> >> "EqDev" <eqdev@newsgrou ps.nospam> wrote in message
            >> >> news:89F37045-D51F-433D-AB79-8B24A436B63A@mi crosoft.com...
            >> >> >I have a class that is a control derived from UserControl. I want[/color][/color][/color]
            to[color=blue][color=green][color=darkred]
            >> >> >use
            >> >> > serialization and deserialization with this calss but I get an
            >> >> > exception
            >> >> > "Cannot serialize member System.Componen tModel.Componen t.Site of[/color][/color][/color]
            type[color=blue][color=green][color=darkred]
            >> >> > System.Componen tModel.ISite because it is an interface.".
            >> >> >
            >> >> > I am not interested in serializing any member from the base class[/color][/color][/color]
            only[color=blue][color=green][color=darkred]
            >> >> > the
            >> >> > properties in the derived class.
            >> >> >
            >> >> > How can I prevent the entire base class from being serialized?
            >> >> >
            >> >> >
            >> >> > Code snippet
            >> >> >
            >> >> > public partial class UserControl1 : UserControl
            >> >> > {
            >> >> > private int item1;
            >> >> > private string item2;
            >> >> >
            >> >> >
            >> >> > public UserControl1()
            >> >> > {
            >> >> >
            >> >> > }
            >> >> >
            >> >> > public int Item1
            >> >> > {
            >> >> > get {return item1;}
            >> >> > set {item1 = value;}
            >> >> > }
            >> >> >
            >> >> > public string Item2
            >> >> > {
            >> >> > get { return item2; }
            >> >> > set { item2 = value; }
            >> >> > }
            >> >> > }
            >> >> >
            >> >> >
            >> >> > public partial class Form1 : Form
            >> >> > {
            >> >> > private UserControl1 Uctrl1;
            >> >> > .
            >> >> > .
            >> >> > .
            >> >> > Uctrl1 = new UserControl1();
            >> >> > Uctrl1.Item1 = 3;
            >> >> > Uctrl1.Item2 = "This is a test";
            >> >> >
            >> >> > .
            >> >> > .
            >> >> > .
            >> >> > // error on line below
            >> >> > XmlSerializer mySerializer = new
            >> >> > XmlSerializer(t ypeof(UserContr ol1));
            >> >> > // To write to a file, create a StreamWriter object.
            >> >> > StreamWriter myWriter = new StreamWriter("m yFileName.xml") ;
            >> >> > mySerializer.Se rialize(myWrite r, Uctrl1);
            >> >> > myWriter.Close( );
            >> >> > }
            >> >> >
            >> >> >
            >> >> > Reposted with proper MSDN alias
            >> >> > --
            >> >> > EqDev
            >> >>
            >> >>
            >> >>[/color]
            >>
            >>
            >>[/color]
            >[/color]

            Comment

            Working...