dinesh
dinesh

Reputation: 735

Serialization in C#

I have a doubt in serialization.

example:

interface IBase {}

[DataContract]
class Base : IBase
{
  [DataContract]
  public Derived Child{get;set;}
}

[DataContract]
[KnownType(typeof(Base))]
class Derived : Base
{
  [DataMember]
  public IBase Parrent {get;set;}
}

If I try to store an instance of Base class in IsolatedStorage, it is not getting executed; it hangs. Is there any way to do this ?

Upvotes: 3

Views: 580

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063403

Well, your attributes are all over the place - Base.Child is a [DataMember], not a [DataContract]. IIRC in phone 7 you need public types for serialization; and it is the base that needs informing of children. However; the IBase is another problem; that isn't designated as a contract. If possible, change that to Base (not IBase), as the serializer needs to know about all concrete contracts.

So:

public interface IBase {}

[DataContract]
[KnownType(typeof(Derived))]
public class Base : IBase
{
    [DataMember]
    public Derived Child{get;set;}
}

[DataContract]
public class Derived : Base
{
    [DataMember]
    public Base Parent {get;set;}
}

Additionally, a Parent member is a big problem for tree serializers (and DataContractSerializer is a tree serializer, unless you explicitly enable full-graph mode).

You might be able to get DCS to like IBase, but you'd need to investigate marking that as a contract and noting the concrete types. If you can't get anywhere with that, I know protobuf-net supports that layout (although I haven't tested that specifically for phone 7, but there is no fundamental reason it can't work).

Upvotes: 5

Related Questions