Solved

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

Posted on 2008-10-29
2
4,275 Views
Last Modified: 2012-05-05
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!!
0
Comment
Question by:creatiive
2 Comments
 
LVL 3

Accepted Solution

by:
torrie01 earned 500 total points
ID: 22830922
Unfortunately you cannot serialize/deserialize an interface because there is no implementation in the interface itself.  Your 'myItem' class implements the 'Imyitem', but any other class could also implement that interface, so the serializer would not know which implementation to use.  Instead, make your List a list of myItem objects (public List<myItem> items) and it should deserialize fine.
0
 
LVL 5

Expert Comment

by:johnaryan
ID: 22831517
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question