473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Serialize a generic collection of interfaces?

XML serialising this class:

[Serializable]
public class Table
{
public List<IActionActions;
public List<ColumnColumns;
}

I think the interface is the problem.
Something in my head tells me that it shouldn't be possible to
serialise the IAction.
Interfaces can't be declared with attributes.

EXCEPTION: System.InvalidOperationException
There was an error reflecting type 'TestProj.Table'

Any suggestions/workarounds ?

Dec 23 '06 #1
3 3863
Cast it to the data type that can be serialized. Perhaps encapsulate each
column with your own object and so.

chanmm

"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
XML serialising this class:

[Serializable]
public class Table
{
public List<IActionActions;
public List<ColumnColumns;
}

I think the interface is the problem.
Something in my head tells me that it shouldn't be possible to
serialise the IAction.
Interfaces can't be declared with attributes.

EXCEPTION: System.InvalidOperationException
There was an error reflecting type 'TestProj.Table'

Any suggestions/workarounds ?
Dec 25 '06 #2
The columns weren't the problem.
Commenting out the IAction list did fix it.
So to get around the problem I had a base "action" object, and instead
of having my Actions implement the IACtion interface, they inherit from
the Action base object instead, which has a number of abstract methods.
The overall result is the same for me, but I would have preferred
interfaces.

For those viewing this solution in the future, you also need to include
an XmlInclude attribute on the actual base class indicating what
inheriting types are acceptable for serialization.

Dec 26 '06 #3
Steven Nagy, 26.12.2006 03:51:
The columns weren't the problem.
Commenting out the IAction list did fix it.
So to get around the problem I had a base "action" object, and instead
of having my Actions implement the IACtion interface, they inherit from
the Action base object instead, which has a number of abstract methods.
The overall result is the same for me, but I would have preferred
interfaces.

For those viewing this solution in the future, you also need to include
an XmlInclude attribute on the actual base class indicating what
inheriting types are acceptable for serialization.
Maybe I am wrong, but think about it the other way round: How would you
deserialize the data? Since you have nothing more than a collection of
interfaces, which class (implementing that interface) should be created?

You could go the hard way and de-/serialize your list by hand using
XmlWriter and XmlReader. I had to do this recently because I needed to
XMLize a private hashtable. It turned out to be a really bad idea. :)

However, with a bit of (Type).GetCustomAttributes(XmlWhateverAttribute,
true) and some coffee things worked fine. I wrote an article about the
technique to extract the XmlEnumAttribute from an enumeration member
some days ago. It is in german but the code should be easy to
understand. You can find it here: http://mac.defx.de/archiv/134

Regards,
Markus
Dec 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
0
by: Tim_Mac | last post by:
hi, i am able to serialize a normal generic List<> no problem. But when i try to serialize a Dictionary<string,string>, i get an exception as shown below: "The type...
1
by: HC | last post by:
I've stripped down my code to barebones to demonstrate (below). I have interfaces and classes which implement them. I force my generic collection to accept only the interface type. If I create an...
3
by: Mirek Endys | last post by:
What is the best way to serialize object, that contains data in interfaces. Simple. I have instance of my object, that contains data in interfaces. What is the best way to XMLSerialize and...
2
by: Jinsong Liu | last post by:
I have following 3 classes public class MyMainClass { MyCollection<MyObject> m_oMyObjectCollection = null; private string m_sID = string.Empty; public MyCollection<MyObject> Collection {
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
1
by: rh1200la | last post by:
Hey all. Here's my question. I'm building a Shipping rates component with the following classes: Package which contains basic package information. I also have shipper classes that implement...
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
1
by: Tony Johansson | last post by:
Hello! Assume I have a class called Product which is defined with the attribute I create a collection by using the generic class List in this way List<Productproducts = new List<Product>(); ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.