Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

15 Experts available now in Live!

Get 1:1 Help Now