Question

I am aware about the problem when creating custom collections that inherits from List<T> or ICollection<T> with additional custom properties:

public class MyCollection: List<int>
{
    public string MyCustomProperty { get; set; }
}

As I know, such collection will passed throw WCF as ArrayOfInt and WCF will not serialize my custom property. The solution is to create the wrapper class that will manage collection inside and will have a custom property.

I want to make a nicer workaround for my needs...does IEnumerable<T> will have the same problem?

public class MyCollection: IEnumerable<int>
{
   /**************/
   /* Code that implements IEnumerable<int> and manages the internal List<T> */
   /* I know I will not able to cast it to List<T>, but I don't need it.  */
   /* If I will need it, I will implement cast operators later */
   /**************/

   public string MyCustomProperty { get; set; }
}

Will the class above pass throw WCF include MyCustomProperty value?

Thanks

Was it helpful?

Solution

I tried it and it does not serialize custom property. I just returned the entire class object from the service method. Result is still ArrayOfInt (i used List as container)

public class MyExtension: IEnumerable<int>
{
    public string CustomString { get; set; }
    private List<int> lst = new List<int>(); 

    public void Add(int i)
    {
        lst.Add(i);
    }

    public IEnumerator<int> GetEnumerator()
    {
        return lst.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return lst.GetEnumerator();
    }
}

I had to mark it as DataContract and every member as DataMember to have all the properties serialized.

<MyExtension xmlns="http://schemas.datacontract.org/2004/07/GetRequestTest"     xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <CustomString>sunny</CustomString> 
 <lst xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <a:int>1</a:int> 
 </lst>
</MyExtension>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top