Link to home
Start Free TrialLog in
Avatar of creatiive
creatiive

asked on

"Cannot serialize because it is an interface." exception message.

Hi,

I am trying to DE-serialize an object,  but when i attempt to do so, i get the error:  "Cannot serialize member 'items'  of type 'ImyItem'   because it is an interface.  The class member it is referring to is ;

public List<ImyItem> items {
    get { return this._items; }
    set { this._items = value; }
}

the interface 'ImyItem' defines the member variables and a few methods for the 'my'Item' objects.  Why am i getting this error on deserialization?  The code to deserialize  that i am using is;

System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
    XmlSerializer ser = new XmlSerializer(typeof(WS.MyClass));
        MyClass ob = (MyClass)ser.Deserialize(sr);

        System.console.writeline(ob.name.tostring());
        sr.Close();


Any help with this would be greatly appriciated!!
ASKER CERTIFIED SOLUTION
Avatar of torrie01
torrie01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can only Serialise concrete types. This is an annoying limitation of Serialization and one that means you must consider your Design Pattern before you implement any code.

If possibe, you should serialise the contrete class that underlies then ImyItem to get it to work.

J