与DataContractSerialiser和序列化问题 [英] Problems with the DataContractSerialiser and Serializable

查看:258
本文介绍了与DataContractSerialiser和序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几节课从asembly至极IS .NET 2.0。这些类makred与序列化。

I've a few Classes from a asembly wich is .Net 2.0. These Classes are makred with Serializable.

在我的项目,我使用这个类在我的课,至极标有DataContract(IsReference = TRUE)和DataMember。

In my Project, i uses this classes in my Classes, wich are marked with DataContract(IsReference=true) and DataMember.

现在我有问题,与DataContractSerialiser它串行化我的.Net 2.0类的私人领域,至极将无法工作。但是当我使用XMLSerialiser,我不能使用IsReference,所以我也无法做到这一点。

Now I have the Problem, with DataContractSerialiser that it serialises the private fields of my .Net 2.0 Classes, wich will not work. But when I use XMLSerialiser, i cannot use IsReference, and so I can also not do this.

有一个简单的(简单)Solutiuon这个?也许有人有自己的XMLSerializer至极支持参考?

Is there a easy (simple) Solutiuon for this? Maybe a someone has a own XMLSerializer wich supports references?

她是一个有点我的代码......

Her is a bit of my code....

[DataContract(IsReference = true)]
public class ConnectionConfig: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
            PropertyChanged(this, new PropertyChangedEventArgs("ObjectAsString"));
        }
    }

    private PLCConnectionConfiguration _configuration;
    [DataMember]
    public PLCConnectionConfiguration Configuration
    {
        get { return _configuration; }
        set { _configuration = value; NotifyPropertyChanged("Configuration"); }
    }
}

其中PLCConnectionConfiguration来自.NET 2.0 asembly,并装饰有[可序列化]

where PLCConnectionConfiguration comes from a .NET 2.0 asembly, and is decorated with [Serializeable]

推荐答案

也许你可以使用的XmlSerializer并指定XmlAttributeOverrides忽略私有字段你不感兴趣在:

Perhaps you could use XmlSerializer and specify XmlAttributeOverrides to ignore the private fields you're not interested in:

var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(PLCConnectionConfiguration), <name of private field to exclude>, new XmlAttributes { XmlIgnore = true });
...
var serialiser = new XmlSerializer(typeof(ConnectionConfig), overrides);

您会显然也必须标记你的ConnectionConfig为Serializable并向_configuration字段中添加[XmlIgnore]

You'd obviously also have to mark your ConnectionConfig as Serializable and add [XmlIgnore] to the _configuration field.

这篇关于与DataContractSerialiser和序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆