0

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

Alex Dn
  • 5,465
  • 7
  • 41
  • 79

1 Answers1

1

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>
vibhu
  • 1,657
  • 2
  • 15
  • 19
  • But after you marked it as DataMember, it is returns a custom property, so it is solution, since with ICollection, the property not passes even when marked as DataMember. – Alex Dn Jun 27 '13 at 04:41
  • By the way, I think I found that if a class will be marked as Serializable and not DataContract, it will work to and even with ICollection, but I'm not tested it well yet. – Alex Dn Jun 27 '13 at 04:42
  • And I believe you make a property for List to be able to mark it DataMember, since private fields not serialized with DataContract. Or I'm wrong? – Alex Dn Jun 27 '13 at 04:48
  • private fields are serializable with DataMember attribute. And yes you can get serialization done through Serializable attribute but i think DataContract is faster. Pls. refer to this post - http://stackoverflow.com/questions/4792269/whats-the-advantage-to-use-datacontract-rather-than-serializable-in-wcf – vibhu Jun 27 '13 at 07:01