serialization prob

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frazer

    serialization prob

    using System;
    using System.Runtime. Serialization;
    using System.IO;
    using System.Runtime. Serialization.F ormatters.Binar y;


    namespace MySerialize
    {
    /// <summary>
    /// Summary description for MySerialize.
    /// </summary>
    [Serializable]
    public class MySerialize: ISerializable
    {
    string name = "Sheraze";
    private MySerialize()
    {
    }
    public MySerialize(str ing name )
    {
    this.name = name;
    }
    void ISerializable.G etObjectData(Se rializationInfo info,StreamingC ontext
    context)
    {
    info.SetType(ty peof(MySerializ e));
    }
    }
    public class MyTestApp
    {
    [STAThread]
    public static void Main()
    {
    FileStream fs = new FileStream("mys erialize.txt", FileMode.Create );
    BinaryFormatter formatter = new BinaryFormatter ();
    MySerialize[] obj1 = {new MySerialize("Na me1") , new MySerialize("Na me2")};
    formatter.Seria lize(fs, obj1);
    fs.Position = 0;
    MySerialize[] obj2 = (MySerialize[]) formatter.Deser ialize(fs);
    fs.Close();
    }
    }
    }



    i get an error on this line saying
    MySerialize[] obj2 = (MySerialize[]) formatter.Deser ialize(fs);

    Additional information: The constructor to deserialize an object of type
    MySerialize.MyS erialize was not found.
    why is that and how do i resolve it?
    thnx


  • Stuart Gunter

    #2
    Re: serialization prob

    As far as I know, serialization requires a public default constructor,
    whereas yours is private.

    Maybe give this a try and see what happens.

    Can anyone confirm this?


    "frazer" <ichor@hotmail. com> wrote in message
    news:O$DxW0EREH A.2408@tk2msftn gp13.phx.gbl...[color=blue]
    > using System;
    > using System.Runtime. Serialization;
    > using System.IO;
    > using System.Runtime. Serialization.F ormatters.Binar y;
    >
    >
    > namespace MySerialize
    > {
    > /// <summary>
    > /// Summary description for MySerialize.
    > /// </summary>
    > [Serializable]
    > public class MySerialize: ISerializable
    > {
    > string name = "Sheraze";
    > private MySerialize()
    > {
    > }
    > public MySerialize(str ing name )
    > {
    > this.name = name;
    > }
    > void ISerializable.G etObjectData(Se rializationInfo info,StreamingC ontext
    > context)
    > {
    > info.SetType(ty peof(MySerializ e));
    > }
    > }
    > public class MyTestApp
    > {
    > [STAThread]
    > public static void Main()
    > {
    > FileStream fs = new FileStream("mys erialize.txt", FileMode.Create );
    > BinaryFormatter formatter = new BinaryFormatter ();
    > MySerialize[] obj1 = {new MySerialize("Na me1") , new[/color]
    MySerialize("Na me2")};[color=blue]
    > formatter.Seria lize(fs, obj1);
    > fs.Position = 0;
    > MySerialize[] obj2 = (MySerialize[]) formatter.Deser ialize(fs);
    > fs.Close();
    > }
    > }
    > }
    >
    >
    >
    > i get an error on this line saying
    > MySerialize[] obj2 = (MySerialize[]) formatter.Deser ialize(fs);
    >
    > Additional information: The constructor to deserialize an object of type
    > MySerialize.MyS erialize was not found.
    > why is that and how do i resolve it?
    > thnx
    >
    >[/color]


    Comment

    • frazer

      #3
      Re: serialization prob

      yup u are right and it works fine now..when i try to Serialize and
      deserialize strings.

      but i now want to serialize and deserialize a dataset object.
      (UserPreference DataSet) that i created myself.
      how do i do that?

      I am able to serialize using
      void ISerializable.G etObjectData(Se rializationInfo info, StreamingContex t
      context)
      {
      info.AddValue(" DataSet", this.userPrefer enceDataSet);
      }

      but when i deserialize , i get an error.
      public UserPreference( SerializationIn fo info, StreamingContex t context)
      {
      this.userPrefer enceDataSet = (UserPreference DataSet)
      info.GetValue(" DataSet", this.userPrefer enceDataSet.Get Type());
      }

      Exception has been thrown by the target of an invocation.

      I think maybe the reason is that info.GetValue doesnt take a dataset type
      as a parameter.
      i looked into a dataset created internally by dotnet.


      protected UserPreferenceD ataSet(Serializ ationInfo info, StreamingContex t
      context) {
      string strSchema = ((string)(info. GetValue("XmlSc hema", typeof(string)) ));
      if ((strSchema != null)) {
      DataSet ds = new DataSet();
      ds.ReadXmlSchem a(new XmlTextReader(n ew System.IO.Strin gReader(strSche ma)));
      if ((ds.Tables["UserPreference "] != null)) {
      this.Tables.Add (new UserPreferenceD ataTable(ds.Tab les["UserPreference "]));
      }
      this.DataSetNam e = ds.DataSetName;
      this.Prefix = ds.Prefix;
      this.Namespace = ds.Namespace;
      this.Locale = ds.Locale;
      this.CaseSensit ive = ds.CaseSensitiv e;
      this.EnforceCon straints = ds.EnforceConst raints;
      this.Merge(ds, false, System.Data.Mis singSchemaActio n.Add);
      this.InitVars() ;
      }
      else {
      this.InitClass( );
      }
      this.GetSeriali zationData(info , context);
      System.Componen tModel.Collecti onChangeEventHa ndler schemaChangedHa ndler =
      new System.Componen tModel.Collecti onChangeEventHa ndler(this.Sche maChanged);
      this.Tables.Col lectionChanged += schemaChangedHa ndler;
      this.Relations. CollectionChang ed += schemaChangedHa ndler;
      }

      do i have to deserialize using this code as a guide?
      or is there a simpler way to deserialze a dataset?
      thanx




      Comment

      • frazer

        #4
        Re: serialization prob

        whoops,,
        it works fine.
        i had a typo in my calling program
        thanx for your time


        "frazer" <ichor@hotmail. com> wrote in message
        news:uQ49kYHREH A.1892@TK2MSFTN GP09.phx.gbl...[color=blue]
        > yup u are right and it works fine now..when i try to Serialize and
        > deserialize strings.
        >
        > but i now want to serialize and deserialize a dataset object.
        > (UserPreference DataSet) that i created myself.
        > how do i do that?
        >
        > I am able to serialize using
        > void ISerializable.G etObjectData(Se rializationInfo info, StreamingContex t
        > context)
        > {
        > info.AddValue(" DataSet", this.userPrefer enceDataSet);
        > }
        >
        > but when i deserialize , i get an error.
        > public UserPreference( SerializationIn fo info, StreamingContex t context)
        > {
        > this.userPrefer enceDataSet = (UserPreference DataSet)
        > info.GetValue(" DataSet", this.userPrefer enceDataSet.Get Type());
        > }
        >
        > Exception has been thrown by the target of an invocation.
        >
        > I think maybe the reason is that info.GetValue doesnt take a dataset type
        > as a parameter.
        > i looked into a dataset created internally by dotnet.
        >
        >
        > protected UserPreferenceD ataSet(Serializ ationInfo info, StreamingContex t
        > context) {
        > string strSchema = ((string)(info. GetValue("XmlSc hema", typeof(string)) ));
        > if ((strSchema != null)) {
        > DataSet ds = new DataSet();
        > ds.ReadXmlSchem a(new XmlTextReader(n ew[/color]
        System.IO.Strin gReader(strSche ma)));[color=blue]
        > if ((ds.Tables["UserPreference "] != null)) {
        > this.Tables.Add (new UserPreferenceD ataTable(ds.Tab les["UserPreference "]));
        > }
        > this.DataSetNam e = ds.DataSetName;
        > this.Prefix = ds.Prefix;
        > this.Namespace = ds.Namespace;
        > this.Locale = ds.Locale;
        > this.CaseSensit ive = ds.CaseSensitiv e;
        > this.EnforceCon straints = ds.EnforceConst raints;
        > this.Merge(ds, false, System.Data.Mis singSchemaActio n.Add);
        > this.InitVars() ;
        > }
        > else {
        > this.InitClass( );
        > }
        > this.GetSeriali zationData(info , context);
        > System.Componen tModel.Collecti onChangeEventHa ndler schemaChangedHa ndler =
        > new[/color]
        System.Componen tModel.Collecti onChangeEventHa ndler(this.Sche maChanged);[color=blue]
        > this.Tables.Col lectionChanged += schemaChangedHa ndler;
        > this.Relations. CollectionChang ed += schemaChangedHa ndler;
        > }
        >
        > do i have to deserialize using this code as a guide?
        > or is there a simpler way to deserialze a dataset?
        > thanx
        >
        >
        >
        >[/color]


        Comment

        Working...