all groups > dotnet windows forms designtime > august 2006 >
You're in the

dotnet windows forms designtime

group:

Using CodeDomSerializer to save application settings


Using CodeDomSerializer to save application settings Iain Macnab
8/9/2006 3:14:22 PM
dotnet windows forms designtime:
Hi all

I am wanting to use the CodeDomSerializer to serialise my applications
object graph. The object graph is manipulated using the PropertyGrid
component and I want to serialize this to disk and then reconstitute it. I
have managed to use the CodeDomSerializer to serialize the object fine via:

MyCustomType m_Target = new MyCustomType()
DesignerSerializationManager m = new DesignerSerializationManager();
m.CreateSession();
CodeDomSerializer s = (CodeDomSerializer)m.GetSerializer(m_Target.GetType(),
typeof(CodeDomSerializer));
CodeStatementCollection result = (CodeStatementCollection)s.Serialize(m,
m_Target);

I have two questions:

Firstly - How can I serialize this to file in a readable format. I have
tried using the SOAPFormatter but the output is so verbose it is unusable (I
want it to be human readable and editable). I have also tried using the
CSharpCodeProvider to output it as c# code. I prefer this method but am
stuck as to how to reconstitute it from the code once generated.

Secondly - when I call the Deserialize method it throws the following error.
"Could not find type 'SWIP.Utility.MyCustomType. Please make sure that the
assembly that contains this type is referenced. If this type is a part of
your development project, make sure that the project has been successfully
built.". All of the examples on the web show no custom deserialization.


I will be eternally greatful if somebody can prove their .Net expertise and
help me out with this problem.

Thanks in advance
RE: Using CodeDomSerializer to save application settings v-lliu NO[at]SPAM online.microsoft.com
8/10/2006 12:00:00 AM
Hi,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
RE: Using CodeDomSerializer to save application settings v-lliu NO[at]SPAM online.microsoft.com
8/11/2006 1:36:38 PM
Hi,

I performed a test based on your sample code and encountered the same
problem as you did. When the problem is running to the code "object obj =
s.Deserialize(m, result);", an error "Could not find the type MyCustomType.
Please make sure that the assembly that cotains this type is referenced.
....." occurs.

I think the IDesignerSerializationManager interface and CodeDomSerializer
class should to be used in the design-time scenario. For example, when you
drag&drop your custom component to a form, the
IDesignerSerializationManager and CodeDomSerializer will help generate the
corresponding code of creating and initializing such an object in the
form's code file.

However, in your scenario, what you want is to serialize a custom type
object to a file and then deserialize a new object from the file. I think
you may use SoapFormatter class to do this. This sample below requires you
set up a Windows application, add a reference to
System.Runtime.Serialization.Formatters.Soap assembly in the project and
add two buttons on the form, named btnSerialize and btnDeSerialize
respectively. You should also mark the custom type MyCustomType
serializable. To do this, add a SerializableAttribute to the MyCustomType,
which looks like below:

[Serializable]
public class MyCustomType
{ .........}

The following is a sample to serialize an object to a file and deserialize
it from the file.

using System.Runtime.Serialization.Formatters.Soap;
public partial class Form1 : Form
{
private void btnSerialize_Click(object sender, EventArgs e)
{
MyCustomType m_Target = new MyCustomType();
FileStream fs = new FileStream("myfile.txt", FileMode.Create);

SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fs, m_Target);
fs.Close();
}

private void btnDeSerialize_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("myfile.txt", FileMode.Open);
SoapFormatter formatter = new SoapFormatter();
MyCustomType obj = formatter.Deserialize(fs) as MyCustomType;
fs1.Close();

MessageBox.Show(obj.IntProperty.ToString() + " " +
obj.StringProperty);

}
}

When the program is running and you click the btnSerialize, a file named
"myfile.txt" is generated. Open this file, you will see the object is
serialized into information of xml format, which is human readable and
editable.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
RE: Using CodeDomSerializer to save application settings v-lliu NO[at]SPAM online.microsoft.com
8/15/2006 10:49:39 AM
Hi,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If the problem is not resolved or you have anything unclear, please feel
free to tell me.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
RE: Using CodeDomSerializer to save application settings Iain Macnab
8/21/2006 1:55:01 AM
Linda

Thank you for your reply. Apologies for the delay getting back to you. I
have been on holiday. I shall take a look at your sample and get back to you
tomorrow.

Kind Regards
RE: Using CodeDomSerializer to save application settings Iain Macnab
8/22/2006 2:05:02 AM
Linda

Thanks again for your reply and you are correct. I can serialize the object
model using soap serialization but for complicated object models it is not
very readable. I am really trying to recreate the design time behaviour of
visual studio. The lifecycle of the application is to be as follows:

Design Mode
1. Services (which can be dynamically loaded from any library) can be
dragged and dropped on to the application workspace. This is very similar to
the windows form designer using components.
2. These services can be configured using the PropertyGrid control also
similar to the windows forms control.
3. The configuration can be serialized in a similar way to the form
(preferrably as code instructions).
4. If design mode is run again all previously saved components are recreated
in the workspace (as per the visual studio GUI).

Run Mode
1. The application recreates the object model which is globally available
to the application components.

I should also add that the base class being serialized will always be of a
known type (much like visual studio in that it is a form or a control etc.).

I realise that there is an overlap in that I am trying to achieve the power
of design time behaviour to store application settings but essentially it is
an application that has both a design time mode and a runtime mode.

I prefer the use of the code serialization as per visual studio as it is
less verbose, easier to understand and easier to edit. Could you point me in
the direction of how a form is recreated from the .cs file in the visual
studio environment (it does this even before the class has been compiled). I
am sure that there must be the necessary classes in the .Net runtime. If not
I guess that I will have to fall back to the SOAP method but I would really
prefer to figure this out.

Thanks in advance

RE: Using CodeDomSerializer to save application settings Iain Macnab
8/24/2006 12:51:02 PM
Thanks Linda

This example has given me a solution to the problem. Thanks for all of your
help with this.

cheer
RE: Using CodeDomSerializer to save application settings v-lliu NO[at]SPAM online.microsoft.com
8/24/2006 1:27:54 PM
Hi Lain,

I understand what you want in the aspect of Design Mode, but I am sorry
that I couldn't understand what you mean in the aspect of Run Mode.

As for the Design Mode, what you want to do is to mimic the design time
behavior of VS IDE. But I don't think it is an easy work.

If you are using VS.NET 2003, to create a custom designer, you should
implement IDesignerHost, IContainer interfaces and implement the services
such as IToolboxService, IDesignerExtentService, ISelectionService,
IMenuCommandService, etc,

You may refer to the following link for more information on how to create a
custom designer in VS.NET 2003.

http://msdn.microsoft.com/msdnmag/issues/04/12/CustomFormsDesigner/#S2

If you are using VS 2005, you should derive a new class from
DesignerSurface, a new class from DesignerSurfaceManager and implement the
services such as ImenuCommandService, IToolboxService, etc.

For more information on how to create a custom designer in VS 2005.

http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support
Re: Using CodeDomSerializer to save application settings Danny Wang
10/6/2006 2:53:19 PM
[quoted text, click to view]
Hi Linda,
I apologize to jump in the discussion. I wonder if you can give me
some hints on one issue related to this topic.
I found the article you recommended
http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/
is very useful to my project. However, there is one question I haven't
figured out: the propertyGrid in that example doesn't have "Event" tab.
Therefore, it cannot add any custom action/function to the form. I
wonder if there is an easy way to enable this?
Thanks,
AddThis Social Bookmark Button