NameValueCollections in WebMethod

C

Chris Arnold

Hi All,

Has anyone been able to return a
System.Collection.Specialized.NameValueCollection object from a WebMethod?

Compiles correctly, but then throws an InvalidOperationException on viewing
the asmx file...

"You must implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits from
ICollection."

Fair enough, but ICollection does not require an Add method and neither does
IEnumerable (which ICollection extends).

Is this just a quirk of some strange serialization the CLR is attempting to
perform?

Chris
 
J

Josh Twist

Hi Chris,

Yes - it's because the NameValueCollection isn't XmlSerializable.

"To be XML serializable, types which inherit from ICollection must have
an implementation of Add(System.String) at all levels of their
inheritance hierarchy.
System.Collections.Specialized.NameValueCollection does not implement
Add(System.String)."

You could easily create your own custom collection by inheriting from
CollectionBase (.NET 1.x:
http://msdn.microsoft.com/library/d...systemcollectionscollectionbaseclasstopic.asp)
or using generics (.NET 2.0:
http://msdn2.microsoft.com/en-us/library/ms132397(VS.80).aspx). This
should be XmlSerializable without any trouble.

Josh

Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top