implement default accessor on queue

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
When i try to do a gettype for the xmlserializer for a Queue(of CustomType). it give me an error asking me to implement a default accessor on generic.queue because it inherits from icollection. How do i do that? I know i could convert it to an array first but can you just do it with a queue?
 
Because of the way the Queue (and many other "specialized" generic collections) is implemented, there is no default accessor to it. For example you cannot do a MyVar = MyQueue(3). Because of this, you cannot serialize it this way. Easiest is as you said copy to a list or array before serializing.
 
It can be done, but I don't see any point to it, it will serialize as an xml array regardless. It's a lot less work to just do ToArray and serialize that, and if you need to create queue after deserialization use the New(collection) constructor passing it the array.

To do it you must write a class that inherits Queue(Of T) and add a default indexed Item property (utilizing Enumerable.ElementAt), and an Add method (utilizing Enqueue method), and also duplicate constructors if you want to include base constructors other than the parameterless. Since there is no way to set an item by index with the current interfaces you must either make the Item property Readonly (which is sufficient for XmlSerializer) or in setter do an export/import operation for all items.
 
Back
Top