使用WCF的DataContract / Data成员序列化对象

时间:2011-05-03 15:55:07

标签: c# .net wcf serialization

我正在尝试序列化我的数据层对象。这些是具有属性,方法和对象集合的复杂对象。

所有类都继承自序列化的基类。

    [DataContract]
    [KnownType(typeof(Person))]
    [KnownType(typeof(Client))]
    [KnownType(typeof(Professional))]
    [KnownType(typeof(ProfessionalApptHistory))]
    [KnownType(typeof(DailySchedule))]
    [KnownType(typeof(Option))]
    [KnownType(typeof(PersonType))]
    [KnownType(typeof(ProfessionClientInteractionType))]
    [KnownType(typeof(ProfessionalClientInteractions))]
    [KnownType(typeof(ProfessionalCalendar))]
    public abstract class CMBase
    {
        protected designs3_OurTimeEntities _entities = new designs3_OurTimeEntities();
        [DataMember()]
        public abstract bool IsPersisted { get; internal set; }
    }

我有一个人类也被序列化

    [DataContract(Name = "Person")]
    [KnownType(typeof(Professional))]
    [KnownType(typeof(Client))]
    public abstract class Person : CMBase
    {
        [DataMember()]
        public int PersonId { get; internal set; }
        [DataMember()]
        public int PersonTypeId { get; set; }
        [DataMember()]
        public string Name { get; set; }
        [DataMember()]
        public string ContactNumber { get; set; }
        [DataMember()]
        public string ImageLocation { get; set; }
        [DataMember()]
        public Bitmap Image { get; internal set; }
        [DataMember()]
        public Guid MembershipUserId { get; internal set; }
        [DataMember()]
        public DateTime? ActivationStartDate { get; internal set; }
        [DataMember()]
        public string Email { get; internal set; }
        [DataMember()]
        public string Zip { get; set; }
        [DataMember()]
        public bool ShowDefaultPage { get; set; }
        [DataMember()]
        public bool IsClient { get; internal set; }

        [DataMember()]        
        public override bool IsPersisted
        {
            get
            {
                if (this.PersonId == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            internal set
            {
                throw new NotImplementedException();
            }
        }

...         }

然后我有这个类,它确实被序列化了

    [DataContract(Name = "Professional")]
    public class Professional : Person
    {
        [DataMember()]
        public DateTime DateStartedInProfession { get; set; }
        [DataMember()]
        public int? ShopId { get; set; }
        [DataMember()]
        public DateTime LastPayment { get; set; }
        [DataMember()]
        public DateTime NextPaymentDate { get; set; }
        [DataMember()]
        public DateTime TerminationDate { get; set; }
        [DataMember()]
        public int? Rating { get; internal set; }
        [DataMember()]
        public bool hasSystemAccess { get; internal set; }
        [DataMember()]
        public List<SaveResult> SaveResults { get; internal set; }
...
}

我还有其他类没有被序列化,我不明白发生了什么。

3 个答案:

答案 0 :(得分:1)

查看Serialize a nullable int

您可以遵循一些方法。

答案 1 :(得分:1)

对于迟到的访客(就像我一样): 继承是问题,因为这里讨论: Definiting the Serialization DataMember in Interface and then using it in a class implementing said interface

如果Person是一个接口而Profession将实现该接口,那就可行。这是因为无法继承属性DataMember

答案 2 :(得分:-1)

更改

[DataMember()]
public int? ShopId { get; set; }

[DataMember()]
public Nullable<int> ShopId { get; set; }

我确信Nullable得到了序列化,但我并不是100%确定这是你唯一的问题。