2

I am attempting to Serialize my data layer objects. these are complex objects that have properties, methods and collections of objects.

All the classes are inheriting from a base class Which gets serialized.

    [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; }
    }

I have a person class which also get serialized

    [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();
            }
        }

... }

then I have this class which doenst get serialized

    [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; }
...
}

I also have other classes that don't get serialized and I don't understand what is happening.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ChampChris
  • 1,565
  • 5
  • 26
  • 44

3 Answers3

1

For late visitors (just like me): Inheritance is the problem, as it is discussed here: Definiting the Serialization DataMember in Interface and then using it in a class implementing said interface

If Person would be an interface and Profession would implement that interface, that would work. This is because the attribute DataMember cannot be inherited.

Community
  • 1
  • 1
Gábor Imre
  • 5,899
  • 2
  • 35
  • 48
1

Take a look at Serialize a nullable int

It has some approaches you could follow.

Community
  • 1
  • 1
Rob P.
  • 14,921
  • 14
  • 73
  • 109
-1

Change

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

To

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

I'm sure that Nullable get's serialized, I'm not 100% sure that's your only problem though.

David Rodrigues
  • 622
  • 5
  • 8
  • I changed the nullable types so they are not nullable anymore, so they read like this [DataMember()] public int ShopId { get; set; }. It still is not serializing when I update the service reference. Is there a problem with the base class inheritance? – ChampChris May 03 '11 at 17:17
  • 1
    `int?` is syntactic sugar for Nullable handled by the compiler. There should be no visible difference to the serializer. – Oskar Berggren Aug 18 '14 at 09:21