1

I have a base class called PrivilegeType and it's inherited by various other classes. Everything works fine except for one specific field called PossibleValues which doesn't get serialized/deserialized well.

Base class definition:

[ProtoContract]
[ProtoInclude(13, typeof(PrivilegeEnum))]
public abstract class PrivilegeType {
...
}

PrivilegeEnum definition:

[ProtoContract]
public class PrivilegeEnum : PrivilegeEnumLike<PrivilegeEnumValue> {
...
}

PrivilegeEnumLike<T> definition:

[ProtoContract]
public abstract class PrivilegeEnumLike<T> : PrivilegeType<T>
{
    [ProtoMember(2)]
    public Dictionary<string, PrivilegeEnumValue> PossibleValues;
    ...
}

PrivilegeEnumValue definition:

[ProtoContract]
public class PrivilegeEnumValue
{
    [ProtoMember(1)]
    public string Value;
    [ProtoMember(2)]
    public string Text;
    [ProtoMember(3)]
    public HashSet<PrivilegeEnumValue> ImpliedValues = new HashSet<PrivilegeEnumValue>();
    ...
}

The thing is, that field is defined inPrivilegeEnumLike<T> which is not included in ProtoInclude list itself, but its subclasses are. I can't add PrivilegeEnumLike<> to ProtoInclude list because I guess it doesn't make sense.

Javid
  • 2,755
  • 2
  • 33
  • 60

1 Answers1

1

Here's the rule:

Class hierarchy must be implemented correctly. This means you need to use ProtoInclude on parent classes, NOT necessarily base classes Otherwise, any inherited class between your subclass and baseclass will not get serialized.

What I did:

Base class:

[ProtoContract]
[ProtoInclude(12, typeof(PrivilegeEnumLike<PrivilegeEnumValue>))]
[ProtoInclude(13, typeof(PrivilegeEnumLike<PrivilegeEnumValue[]>))]
public abstract class PrivilegeType

Middle class:

[ProtoContract]
[ProtoInclude(20, typeof(PrivilegeEnum))]
[ProtoInclude(21, typeof(PrivilegeEnumSet))]
public abstract class PrivilegeEnumLike<T> : PrivilegeType<T>

Everything works correctly now. Please let me know if there's any better solution to this.

Javid
  • 2,755
  • 2
  • 33
  • 60