序列化实现 List<T> 以通过 WCF 传输的类




我花了一些时间为我的应用程序编写代码,假设序列化位是其中最简单的部分。几乎双方(客户端和服务器)都完成了,所有我需要做的是传递一个类AccountInfo从服务到客户端…
问题是AccountInfo继承了List,因此[DataContract]属性无效。
我尝试使用[CollectionDataContract]属性,但随后在另一方(客户端)接收的类仅包含通用列表方法,而没有我自定义实现的属性,如GroupTitle…对于这个问题,我已经想出了一个解决方案,但我不知道如何应用。
基本上一切都工作,当我做一个属性,而不是继承一个列表,但我不能绑定这个类到LongListSelector (WP7),因为它不是一个集合类型。我对这三门课很感兴趣。
AccountInfo包含多个实例:
AccountInfoGroup包含多个实例:
AccountInfoEntry(这个不继承列表,因此序列化它没有问题,所有属性都是可访问的)。

有人可以帮助我使用正确的属性序列化和传输这些类使用WCF方法?


下面是其中两个集合类的代码:

public class AccountInfo : List<AccountInfoGroup>
    {
        public AccountInfo()
        {
            UpdateTime = DateTime.UtcNow;
            EntryID = Guid.NewGuid();
        }
        public bool HasItems
        {
            get
            {
                return (Count != 0);
            }
            private set
            {
            }
        }
        public Guid EntryID
        {
            get;
            set;
        }
        public decimal GetTotalCredit()
        {
            decimal credit = 0;
            foreach (AccountInfoGroup acg in this.Where(item => item.Class == AccountInfoEntry.EntryType.Credit))
            {
                acg.Where(item => item.ItemClass == AccountInfoEntry.EntryType.Credit).ToList().ForEach(entry => 
                { credit += entry.Remaining; }
                );
            }
            return credit;
        }
        public bool UsedForCreditComparison = false;
        public DateTime UpdateTime { get; private set; }
    }
    public class AccountInfoGroup : List<AccountInfoEntry>
    {
        public AccountInfoEntry.EntryType Class 
        {
            get;
            private set;
       }
        public string Title
        {
            get
            {
                return AccountInfoEntry.ClassToString(Class);
            }
        }
        public AccountInfoGroup(AccountInfoEntry.EntryType groupClass)
        {
            this.@Class = groupClass;
        }
        public bool HasItems
        {
            get
            {
                return (Count != 0);
            }
            private set
            {
            }
        }
    }


谢谢你的建议…:)

您的示例对于WCF的序列化来说是相当痛苦的。我建议你修改并拥有一个通用的WCF消息模型(这意味着它只包含getter和setter属性,序列化属性)。

如果您在WP7中的LongListSelector绑定中遇到问题,您可能需要将消息转换为WP7对象支持在绑定中使用的实际类型

最新更新