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@xxxxxxxxxxxxxxxxx> wrote in message
news:1323810C-E41B-4E20-A766-EAA726BC8354@xxxxxxxxxxxxxxxx
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:

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@xxxxxxxxxxxxxxxxx> wrote in message
news:89F37045-D51F-433D-AB79-8B24A436B63A@xxxxxxxxxxxxxxxx
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.ComponentModel.Component.Site of type
System.ComponentModel.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(typeof(UserControl1));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, Uctrl1);
myWriter.Close();
}


Reposted with proper MSDN alias
--
EqDev





.



Relevant Pages

  • Re: How to prevent base class (UserControl) form being serialized
    ... but in the ideal situation you would have control over what is and is ... the base class so your control won't have a name, know its visibility, know ... them on the code for the UserControl. ... Just serialize this structure ...
    (microsoft.public.dotnet.general)
  • Re: How to prevent base class (UserControl) form being serialized
    ... My object happens to be derived from a control so I am penalizes for that. ... them on the code for the UserControl. ... derived control while leaving the base class un-touched. ... Just serialize this structure rather ...
    (microsoft.public.dotnet.general)
  • Re: How to prevent base class (UserControl) form being serialized
    ... them on the code for the UserControl. ... to serialize your control so that you can persist it and then reload it at ... Just serialize this structure rather ... I am not interested in serializing any member from the base class only the ...
    (microsoft.public.dotnet.general)
  • Re: Driving me nuts! How to do two-levels inheritance under UserControl?
    ... because your base control is not based on UserControl. ... Create the next level of inheritance exactly like you did the first ... Create a UserControl and edit the designer.vb file to change the ... Inherits line from UserControl to your base class. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Why must I paint the form background for an owner-draw control?
    ... His problem stems from the fact that the button base Class has ... This problem does not appear in a class based upon Control ... that is why there is a black background. ... >> protected override void OnMouseDown ...
    (microsoft.public.dotnet.framework.windowsforms)

Loading