Skip to content

ISerializable problems

Jared

Asked Nov 3, 2008, 11:32 AM EST1 answer
Share
LinkedInX
How can I serialize an object containing an interface?

I was thinking i could inherit from ISerializable and have GetObjectData determine the type at runtime, and serialize that.  unfortunatly i receive an error creating the XMLSerializer
cannot serialize the object because it contains an Interface.

is it possible to get around this and still use the XMLserializer?

here is some example code:
   public interface MyInterface
    {
        void DoStuff();
    }
    public class DataTypeInt : MyInterface
    {
        public int data = 0;
        #region MyInterface Members

        public void DoStuff()
        {
        }
        #endregion


    }
    public class DataTypeString : MyInterface
    {
        public string data;
        #region MyInterface Members

        public void DoStuff()
        {
        }
        #endregion

    }
    public class MyObject : ISerializable
    {
        public MyInterface data;
        public MyObject()
        {
        }

        public MyObject(SerializationInfo info, StreamingContext context)
        {
             string type =(string) info.GetValue("type", typeof(string));
             if (type == "DataTypeInt")
             {
                 DataTypeInt MyData = new DataTypeInt();
                 MyData.data = (int)info.GetValue("data", typeof(int));
                 data = MyData;
             }
             else if (type == "DataTypeString")
             {
                 DataTypeString MyData = new DataTypeString();
                 MyData.data = (string)info.GetValue("data", typeof(string));
                 data = MyData;
             }
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            Type type = data.GetType();
            if (type == typeof(DataTypeInt))
            {
                info.AddValue("type", "DataType");
                DataTypeInt myData = data as DataTypeInt;
                info.AddValue("data", myData.data);
            }
            else if (type == typeof(DataTypeString))
            {
                info.AddValue("type", "DataTypeTwo");
                DataTypeString myData = data as DataTypeString;
                info.AddValue("data", myData.data);
            }
        }

    }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    +1
    Well two thinks  should be said in this context
    first
    Any objects those going to be serialisable must be adorned by the [Serializable] attribute
    [Serilizable]
    public class xxx
    {
       bla bla bla;
    }
    other think, the XmlSerilizer couldn't serilize composed object

    public  class x
    {
      
      bla bla bla;
      public y member; // the y composes the x one
     
    }
    public class y
      {
          bla bla bla;
      }

    It will be worth touse a BinarySerilizer instead
    +1