我可以将DataMember添加到WCF中的CollectionDataContract吗?

Jef*_*tin 13 .net wcf

我有一个我用CollectionDataContract装饰的集合类.集合类还在类上有一个属性,我希望将其传递给服务客户端.我已经尝试将[DataMember]添加到该属性,但是当我更新时它没有将它添加到客户端的类中.

那里的任何WCF专家都有任何帮助吗?

Bor*_*sky 12

我的博客上发布了一个可行的解决方案:

http://borismod.blogspot.com/2009/04/wcf-collectiondatacontract-and.html

UPD:谢谢,你的评论,杰夫.以下是非泛型类的摘要.完整的通用解决方案可以在我的博客的新帖子中找到:http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html


[DataContract(IsReference = true)]    
public class EntityCollectionWorkaround : ICollection 
    {

        #region Constructor
        public EntityCollectionWorkaround()
        {
            Entities = new List();
        } 
        #endregion

        [DataMember]
        public int AdditionalProperty { get; set; }

        [DataMember]
        public List Entities { get; set; }

        #region ICollection Members
          // Implement here members of ICollection by wrapping Entities methods
        #endregion

        #region IEnumerable Members
          // Implement here members of IIEnumerable by wrapping Entities methods
        #endregion

    }

  • 很好的博客文章,但如果你在这里给出一个快速摘要,那将是很好的. (2认同)
  • +1是一个很好的解决方案!我太困惑了以为我需要使用`CollectionDataContract`来集合序列化.使用`IXmlSerializable`我将不得不手动处理大量的序列化,"DataContractSerializer"对我来说毫无价值.这个答案对我来说很难找到,所以我用你的答案详细阐述了我的解决方案,并将其作为下面的新答案发布.希望以后其他人更容易找到. (2认同)
  • 我很高兴您发现我5岁的答案很有用:-) (2认同)