开发者

Silverlight serialize object with cycles in object graph

I'm having an issue serializing objects when sending them to my WCF services. My classes look like this.

public class Foo
{
     public Bar Bar { get; set; }
}

public class Bar
{
    public Foo Bar { get; set; }
}

This causes a cycle in my object graph. I'v开发者_StackOverflowe fixed this on the server end by using the PreserveReferencesOperationBehavior. However, I still get an error when I try to serialize the objects in Silverlight.

While I can mark my objects with [DataContract(IsReference = true)], I'd prefer not to use this method because I have a large number of classes, many of which have over 100 properties and I don't want to have to add the [DataMember] attribute to each property.

Is there any other way to tell Silverlight to preserve references?

If it matters at all, I am using EntityFramework 4 with Code First.


The infered DataContract behaviour of the serializer is present to assist in the simple DTO scenarios. If you want to do it "properly" you should be using the DataContract and DataMember attributes.

When you find you have anything other than the most simple of scenarios you just need to do things properly. The correct and only way to handle circular references is with IsReference.

Lesson here is that helpful magic pixie dust only goes so far after that you just need to put in the graft. Sorry its not the answer you were looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜