Solved

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

Posted on 2008-10-29
2
4,260 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now