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 / .NET Framework / .NET SDK / January 2004

Tip: Looking for answers? Try searching our database.

Serialization inheritance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ilya Evseev - 27 Nov 2003 22:28 GMT
   Hi, folks!
There is some class in external library
that is declared with [Serializable()] attribute.
I need to create class inherited from them
that should be serializable too.
My class uses fields calculated at deserialization stage,
so it's declared not as following:
   [Serializable()]
   class MyClass : BaseClass { ... }

but as following (look all !!! and ???'s carefully:-):

   class MyClass : BaseClass, ISerializable
   {
       protected int SimpleField;     // serializable
       protected int CalculatedField; // non-serializable

       public MyClass(
           SerializationInfo info,
           StreamingContext ctx)
   //      : base(info, ctx)    // error!!!
       {
           // my stuff
           info.Add("SimpleField", SimpleField);
       }
       public virtual void GetObjectData(
           SerializationInfo info,
           StreamingContext ctx)
       {
   //      base.GetObjectData(info, ctx);  // error!!!
           // my stuff
           SimpleField = info.GetInteger("SimpleField");
           // how to conform next line
           // with calling of inherited deserialization routine???
           CalculatedField = SomethingVerySpecial(SimpleField);
       }
   }

Well, I don't know how to call inherited constructor
and GetObjectData from my own constructor/GetObjectData
appropriately, because this functionality in base class
is generated implicitly and cannot be called by name.

How to solve this problem?

WBR, Ilya
Bennie Haelen - 04 Dec 2003 00:13 GMT
Hi Ilya,

Consider moving the "base" call up as in:

>         public virtual void GetObjectData(
>             SerializationInfo info,
>             StreamingContext ctx) : base(info, ctx)
>         {
..
           }

Regards,

Bennie Haelen

>     Hi, folks!
> There is some class in external library
[quoted text clipped - 42 lines]
>
> WBR, Ilya
Jacek Helka - 13 Jan 2004 08:35 GMT
Hi,
There is a FormatterServices class that can help you.

  MemberInfo[] members =
FormatterServices.GetSerializableMembers(typeof(BaseClass), context);
  object[] data = FormatterServices.GetObjectData(obj, members);
  for(int i = 0; i < members.Length; i++)
   info.AddValue(members[i].Name, data[i]);

Deserialization is similar:

  MemberInfo[] members =
FormatterServices.GetSerializableMembers(typeof(BaseClass), context);
  object[] data = new object[members.Length];
  for(int i = 0; i < members.Length; i++)
   data[i] = info.GetValue(members[i].Name, data[i]);
 FormatterServices.PopulateObjectMembers(obj, members, data);

>     Hi, folks!
> There is some class in external library
[quoted text clipped - 42 lines]
>
> WBR, Ilya

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.