Serialize System.Windows.Forms.Control

M

michelqa

I'm having hard time trying to serialize the object
System.Windows.Forms.Control with 1.X net framework.

For now my latest "guess" look like this :

//...
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

Control ctl;
int Handle=666666; // a button control handle (button1)
ctl = Control.FromHandle(Handle);
XmlSerializer ser = new XmlSerializer(ctl.GetType()); //*** ERROR
HERE***//
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, ctl);
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(sb.ToString());
xdoc.Save("SerializedControl.xml");

But it return the following error :
There was an error reflecting 'System.Windows.Forms.Button'
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping
(Type type, XmlRootAttribute root, String defaultNamespace)

at System.Xml.Serialization.XmlSerializer..ctor(Type type)

at ...blablabla

I want to serialize System.Windows.Forms.Control to a file in an xml
file or in a file in binary format. I really need help on this :(

Thanks
 
M

michelqa

Still trying to serialize the System.Windows.Forms.Control object

The code works great for simple object like size (ctl.Size) or
localtion (ctl.Location) but it's not working for complex object like
the control, Button, ListViewItem, etc... I suppose the xml
serialization cannot be used for object like this.... How can I
serialize such objects/classes?
 
J

Josh Einstein

You don't. They're not serializable as they derive from MarshalByRefObject.

You can always do it yourself with reflection.

Josh Einstein
 
M

michelqa

Could you be a little bit more explicit about using reflection to
"Store" all objects properties?

After reading all the days about Serialization I realize that there is
no workaround to serialize complexe classes like the Control object :(

My goal was to serialize System.Windows.Forms.* object in one
application and transfer it to another to be deserialized.

For now I dont have any other choice that parse each object/properties
One by One and store it in an xml file by myself... You could imagine
the nigthmare to parse all thoses classes like TreeView.Nodes or
ListView.Items. Doing 10 000 line of code to parse each properties
and collection seems a bad idea and we dont even think about the
reparsing the information from the xml file to customly "deserialize"
it. There must be another solution?
 
J

Josh Einstein

ListView.Items. Doing 10 000 line of code to parse each properties
and collection seems a bad idea and we dont even think about the
reparsing the information from the xml file to customly "deserialize"
it. There must be another solution?

No, there's really not. You may want to look into WPF as a platform instead
of Windows Forms. WPF uses XAML to instantiate its UI which of course you
can then serialize back to XAML. (An XML format that defines object
instances and property setters, among other things.)

As for manually serializing using reflection, it's too broad a topic to
cover in a newsgroup post. There's plenty of articles out there if you
Google ".NET Reflection".

But the idea is, you would need to generalize your approach so that it
worked on any object. Now there's potential problems with that, which is why
objects are not serializable by default. For example, simply accessing the
public properties may not be enough to re-instate an object from scratch.
What if the object does not have a public constructor? What if private
fields were not exposed via public properties?

But anyway, the objects and methods you want to look into are:

Object.GetType
Type.GetProperties
PropertyInfo.GetValue
PropertyInfo.SetValue
Activator.CreateInstance

Josh Einstein
 
M

michelqa

Thanks for the detailled explanation!

I finally end up with custom xml serialisation. Application A ask for
a object/properties and application B return it in XML format.

Inspired by the expression editor in the following articile Object Spy
article https://secure.codeproject.com/KB/d...cure.codeproject.com/KB/dotnet/objectspy.aspx
I will manually store object in xml and will have to do a switch for
each possible object since there are not many non serializable object/
collection in System.Windows.Form.Control classes (maybe less than 100
differents).

In my case I want to evaluate object expression and return it in xml.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top