473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.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;
Feb 6 '06 #1
5 3358
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" <eq***@newsgroups.nospam> wrote in message
news:89**********************************@microsof t.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.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

Feb 6 '06 #2
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" <eq***@newsgroups.nospam> wrote in message
news:89**********************************@microsof t.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.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


Feb 6 '06 #3
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" <eq***@newsgroups.nospam> wrote in message
news:13**********************************@microsof t.com...
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" <eq***@newsgroups.nospam> wrote in message
news:89**********************************@microsof t.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.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


Feb 6 '06 #4
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:
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" <eq***@newsgroups.nospam> wrote in message
news:13**********************************@microsof t.com...
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" <eq***@newsgroups.nospam> wrote in message
news:89**********************************@microsof t.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.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


Feb 7 '06 #5
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

--------------------
Thread-Topic: How to prevent base class (UserControl) form being serialized
thread-index: AcYr357YOeoId7sFR96a2R1fkmd/+g==
X-WBNR-Posting-Host: 24.249.212.34
From: "=?Utf-8?B?RXFEZXY=?=" <eq***@newsgroups.nospam>
References: <89**********************************@microsoft.co m> <um**************@TK2MSFTNGP12.phx.gbl>
<13**********************************@microsoft.co m>
<OC**************@TK2MSFTNGP15.phx.gbl>Subject: Re: How to prevent base class (UserControl) form being serialized
Date: Tue, 7 Feb 2006 04:11:27 -0800
Lines: 142
Message-ID: <52**********************************@microsoft.co m>
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.public.dotnet.general
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.general:188179
X-Tomcat-NG: microsoft.public.dotnet.general

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:
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" <eq***@newsgroups.nospam> wrote in message
news:13**********************************@microsof t.com...
> 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" <eq***@newsgroups.nospam> wrote in message
>> news:89**********************************@microsof t.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.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
>>
>>
>>



Feb 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenny Mullican | last post by:
I am using complex types in order to support serialization/deserialization of floating point numbers, since floating points can't be null. I've seen how to suppress attributes that are "not...
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
3
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that...
1
by: Murray Gill | last post by:
Our current solution has a number of ASP.NET pages with very similar functionality. We would like to move the common functions into a base class that inherits from System.Web.UI.Page, and then force...
8
by: darrel | last post by:
This is a follow-up to a question I asked yesterday. I'm loading a UC programatically as such: =================================================== public customContentControl as UserControl...
3
by: Charles Law | last post by:
Sorry for reposting this question, but I did not get a single answer last time, and I'm sure you guys must have some thoughts on the matter. I have a user control which can be dragged and dropped...
2
by: Charles Law | last post by:
I have a base class - ComponentBase - that inherits from UserControl. My class implements IComponentBase, which defines a minimal set of core properties and methods. All my other components inherit...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
2
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.