儿童物业DataContract家长参考 - 如何避免无限序列循环序列、家长、物业、儿童

2023-09-05 04:11:34 作者:嫣然回首泪倾城

在一个WCF服务,我有一种情况,有一个datacontract(对​​象),其中包含子项目(如集合有一个datamemeber属性)的集合。在子集合中的对象有一个参考到母体作为属性。如果父对象子集合为空或包含一个null父引用,一切都很好,但如果家长参考填充和家长的孩子被填充,将其序列,直到永远。

In a WCF service, I have a situation where there is a datacontract (object) that contains a collection of child items (where the collection has a datamemeber attribute). The objects in the child collection have a reference to the parent as a property. If the parent object child collection is empty or contains a null parent reference, all is good, but if the parent reference is populated and parent children are populated, it serializes for ever.

下面是一组测试控制台code给大家展示一下我说的。

Here's a set of test console code to show you what I'm talking about.

class Program
    {
        static void Main(string[] args)
        {

            MyParentObject parent = BuildParent();

            string xml = Serializer<MyParentObject>.Serialize(parent);

            System.Console.Write(xml);

            System.Console.ReadKey();

        }

        static MyParentObject BuildParent()
        {

            MyParentObject parent = new MyParentObject();
            parent.MyParentObjectId = 123;
            parent.MyParentObjectName = "Test Parent Object";
            parent.MyChildren = new List<MyChildObject>();

            for (int i = 0; i < 10; i++)
            {
                MyChildObject child = new MyChildObject();
                child.MyParent = parent;
                child.MyChildObjectId = i;
                child.MyChildObjectName = string.Format("Test Child Name {0}", i.ToString());
                parent.MyChildren.Add(child);
            }

            return parent;

        }

    }


    [DataContract]
    public class MyParentObject
    {

        [DataMember]
        public int MyParentObjectId { get; set; }

        [DataMember]
        public string MyParentObjectName { get; set; }

        [DataMember]
        public List<MyChildObject> MyChildren { get; set; }

    }

    [DataContract]
    public class MyChildObject
    {
        [DataMember]
        public MyParentObject MyParent { get; set; }

        [DataMember]
        public int MyChildObjectId { get; set; }

        [DataMember]
        public string MyChildObjectName { get; set; }

    }

public class Serializer<T>
    {

        public static string Serialize(T entity)
        {

            StringBuilder sb = new StringBuilder();


            DataContractSerializer dcs = new DataContractSerializer(typeof(T));

            using (XmlWriter writer = XmlWriter.Create(sb))
            {

                dcs.WriteObject(writer, entity);
                writer.Flush();
            }

            return sb.ToString();
        }

    }

除了清理出造成这种无限序列循环父/子引用,都是围绕这有什么办法呢?

Other than clearing out child/parent references that cause this infinite serialization loop, are there any ways around this?

编辑:我知道,我可以只删除数据成员属性,但我想保持它,根本就不是无限序列化

I know I can just remove the datamember attribute, but I'd like to keep it, and simply not serialize infinitely.

推荐答案

尝试使用IsReference属性的DataContract属性

Try using the IsReference property in your DataContract attribute

[DataContract(IsReference = true)]