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
Josh Twist - 27 Feb 2006 07:58 GMT
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/default.asp?url=/library/en-us/cpref/html/frlr
fsystemcollectionscollectionbaseclasstopic.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