Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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