C# 序列化时如何维护对象引用

C# 序列化时如何维护对象引用,c#,serialization,C#,Serialization,好吧,我有点搞不清楚这是怎么回事,或者有没有可能。我想序列化子类,但实际上我不想在执行Child.MyParent字段时序列化父对象。。。我只想将引用序列化。这可能吗?我该怎么做 public class Parent { public Child New() { return new Child(this); } } public class Child { public Parent MyParent; public Child(

好吧,我有点搞不清楚这是怎么回事,或者有没有可能。我想序列化子类,但实际上我不想在执行Child.MyParent字段时序列化父对象。。。我只想将引用序列化。这可能吗?我该怎么做

public class Parent
{ 
    public Child New()
    {
        return new Child(this);
    }
}

public class Child
{
    public Parent MyParent;

    public Child(Parent parent)
    {
        MyParent = parent;
    }
}
编辑:我正在使用DataContractSerializer,但我不反对在必要时切换到其他内容。

可以将应用于不希望序列化的字段。比如说,

public class Child
{
    [XmlIgnore]
    public Parent MyParent;

    public Child(Parent parent)
    {
        MyParent = parent;
    }
}
但就序列化对字段的引用而言,您必须提供更多关于如何持久化引用所指向的对象的信息。您不只是序列化
父成员
成员(在您的情况下)的原因是什么?序列化所需的所有公共成员是很常见的

如果您只想使用序列化进行克隆,那么类似这样的操作应该可以:

private static Parent Clone(Parent parent)
{
    Parent parentClone = null;
    lock (m_lock) // serialize cloning.
    {
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, parent);
            stream.Seek(0, SeekOrigin.Begin);
            parentClone = (Parent)formatter.Deserialize(stream);
        }
    }

    return parentClone;
}

听起来您可能需要实现自己的序列化和反序列化功能

这是MSDN的摘录

[Serializable]
    public class Person : ISerializable
    {
        private string name_value;
        private int ID_value;

        public Person() { }
        protected Person(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            name_value = (string)info.GetValue("AltName", typeof(string));
            ID_value = (int)info.GetValue("AltID", typeof(int));
        }

        [SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            info.AddValue("AltName", "XXX");
            info.AddValue("AltID", 9999);
        }

        public string Name
        {
            get { return name_value; }
            set { name_value = value; }
        }

        public int IdNumber
        {
            get { return ID_value; }
            set { ID_value = value; }
        }
    }

孩子可以标记我的父母是暂时的;这可以防止它被序列化,但您“只希望引用被序列化”。你能解释清楚你想要什么吗?当一个对象被序列化和反序列化时,引用实际上没有意义。@Sean我正试图利用序列化来克隆一些对象。基本上,它们将被序列化,然后立即反序列化。我知道有一个ICloneable接口用于此,但我认为这将允许我在一系列不同的类型上执行此操作,而无需在所有类型上实现该接口。我认为最好不要序列化此字段,并在序列化过程之外对其进行管理,但如果可能的话,我想知道怎么做。您使用的是哪种序列化方法?@Brandon如果父级有“哑数据”表示(如id整数或简单字符串),您可以将其序列化。但我也在想为什么你真的想序列化并立即反序列化。这听起来像是一种笨拙的克隆方式。我只是想利用序列化来克隆一个对象。因此,将对象序列化为byte[]变量,然后立即反序列化为新对象。父对象从未序列化。我认为更好的方法可能是找到一种方法,在序列化过程之后重新设置父属性。@BrandonMoore-您可以使用来完成此操作。哦,顺便说一句,我正在尝试克隆子属性,而不是父属性。。。看起来你的例子正好相反。无论如何,我同意IDeserializationCallback将是处理这个问题的唯一明智的方法。当然。。。这个示例是否显示要克隆的父对象并不重要,因为逻辑在两种方式中都是一样的:)如果是立即重新序列化,您不能只设置XmlIgnore属性,那么当您反序列化它时,父对象应该为null,您可以将父属性设置为原始对象的父对象。