【发布时间】:2011-07-13 08:32:52
【问题描述】:
我有一些来自 .Net 2.0 程序集的类。这些类标有 Serializable。
在我的项目中,我在我的 Classes 中使用这些类,它们用 DataContract(IsReference=true) 和 DataMember 标记。
现在我遇到了问题,它使用 DataContractSerialiser 序列化我的 .Net 2.0 类的私有字段,这将不起作用。但是当我使用 XMLSerialiser 时,我不能使用 IsReference,所以我也不能这样做。
是否有一个简单(简单)的解决方案?也许有人有自己的支持引用的 XMLSerializer?
这是我的一些代码:
[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 程序集,并用 [Serializeable] 修饰
【问题讨论】:
-
你的意思是你不希望它序列化私有字段(通常 DataContractSerializer 只序列化公共属性)?或者当他尝试但失败时你得到一个错误?
-
是的,我不想序列化私有字段,但是 DataContractSerializer 在用 [Serializable] 修饰的类上执行此操作
-
本例中的
PLCConnectionConfiguration是什么? -
这是来自另一个议会的课程,我无法更改。这个类是可序列化的,但不能使用 DataContractSerializer,因为它序列化了私有字段! (这是我的问题!)
标签: c# serialization reference datacontractserializer xmlserializer