Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / November 2005

Tip: Looking for answers? Try searching our database.

Code serialization order

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
arjunsingri@gmail.com - 31 Oct 2005 11:49 GMT
Hello,

I want the code to serialize in the following order:

MyClass object2 = new MyClass();
Wrapper object1 = new Wrapper(object2);

but currently the code is being serialized in the following way:

Wrapper object1 = new Wrapper(object2);
MyClass object2 = new MyClass();

This is an error because "object2" is null when it is passed to the
constructor of "Wrapper".

"MyClass" inherits from "System.ComponentModel.Component".

Is there any way to change this order ?

Regards,
Arjun
Vadim Chekan - 31 Oct 2005 22:05 GMT
I can't see any serialization in your example.

> Hello,
>
[quoted text clipped - 17 lines]
> Regards,
> Arjun
Dira - 02 Nov 2005 11:01 GMT
Hi Arjun,

So far as i know, there is no way to control the serialzation order in
the built-in VS serializer.

You can implement your own serializer, that takes care of all specific
features your objects have. (Do you really whant to? and do you really
whant to rewrite it each itme dependencies between objects change?)

The other way is to implement ISupportSerialize with its EndInit().
This function will be called (for the objects that implement it)
after all objects are deserialized. You can postpone all critical
actions (those ones that depend on other objects being already
deserialized) until EdnInit() is called.

Again there is no way to control the calling order of EndInit() for
different objects. Thus, you have two "layers" with uncontrollable
calling order inside each of them: (i) deserialization;
and (ii) EndInit().

----------------------------------------------
Sorry! Sorry!!! only now i understand your problem (I hope).
(But the text above can be still usefull nevertheless. Can?).

I would say, implement a default constructor for Wrapper class
and equip the class with new property WrappedObject, so that
serializer generates:

Wrapper object1 = new Wrapper();
MyClass object2 = new MyClass();
.....
object1.WrappedObject = object2;
.....

The whole logic must be transferred from the old constructor to
the new property. I agree, consistency will suffer, since wrappers
without wrapped objects can be created, but i see no other way.

What makes me feel uncomfortable: Can it be, that you present
a rather simplified version of your problem? A lot of objects in my
programs have other objects as ctor parameters and i've never had
any problems. Maybe the reason is that both classes (MyClass and
Wrapper) are IComponent-s and the VS Serializer treats them
differently (it must).

HTH,
Dima.
Arjun Singri - 03 Nov 2005 15:10 GMT
Hello Dima,

Thanks a lot for that piece of information.
Only "MyClass" is an "IComponent". I suppose the code snippet that i
had written before wasnt very descriptive (wrong example). Consider the
example below.
The serialized code looks like this

class Form1 : System.Windows.Form
{
   private Type1 object1;

   private void InitializeComponent()
   {
       Type2 object2 = new Type2(object1);
       object1 = new Type1();
   }
}

but it should actuallly be:

class Form1 : System.Windows.Form
{
   private Type1 object1;

   private void InitializeComponent()
   {
       object1 = new Type1();
       Type2 object2 = new Type2(object1);
   }
}

"Type1"  is an IComponent.
"Type2"  isnt

Given this, is there any way to control the serialization order ?
Dira - 04 Nov 2005 10:41 GMT
Hello Arjun,

I'm afraid you should change something in the object design.
Not because it is invalid, but because MS/VS can not fitt all
imaginable requirements/wishes.

The VS Serializer (i suppose) considers
IComponents as serialization "roots". It starts with Components
and serializes recursively objects that are "used" by them.
An object is considered to be "used", if it is an argument of a ctor,
a value of a read/write property, a member of an ICollection, etc.
What "using" means can be specified more precise by different
attributes.

I do not understand the reason why the Serializer serializes
"object2" in your example. That is why i can hardly understand
what is "thinks" doing this and how to "convince" it to act
differently.

May it be, that "object2" is used by another component
(one different from "object1") ?
I suppose, there must be somewhere an "owner" of object2
that has a rank of a Component and serializing obvject2
is a consequence.

If I new more about your example.

Another idea were to implement all Wrappers as an extension
properties of some IExtenderProvider. In this case the VS Serializer
knows what to do and serializes in a correct order.

Regards,
Dima.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.