问题
I've a few Classes from a asembly wich is .Net 2.0. These Classes are makred with Serializable.
In my Project, i uses this classes in my Classes, wich are marked with DataContract(IsReference=true) and DataMember.
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.
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"); }
}
}
where PLCConnectionConfiguration comes from a .NET 2.0 asembly, and is decorated with [Serializeable]
回答1:
Annotating a type with [DataContract]
should be enough to tell it to look for the members marked [DataMember]
- but it sounds like maybe you have a combination of data-contracts and vanilla objects.
If you strictly need xml, my recommendation here would be to write a set of DTO types and use DCS in graph-mode. This may require some mapping between your object model and the DTOs; but that is usually not a big problem. Note also that DCS in graph-mode is not typical xml output - it will be very different to what XmlSerializer
would output.
If you just need to serialize arbitrary types (xml not being the issue), then the current experimental cut of protobuf-net supports all these scenarios; it'll handle the object references, and it'll allow you to work with both annotated and vanilla types (you just need to tell it how). If that is an option, perhaps provide a basic example of your model and I might be able to fill in some blanks.
回答2:
You should abstract the WCF datacontracts from your domain models. Consider the datacontracts to be a view of the model that you want to return in your service.
Annotate your datacontract with a [DataContract]
attribute and all the properties you want to expose with a [DataMember]
attribute. If there are properties you don't want to return you just DON'T annotate it with the [DataMember]
attribute.
If you want your domain models to be XmlSerializable you annotate the class with the [Serializable]
attribute.
It may require some additional typing and converting but its clearer overall imho.
回答3:
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);
You'd obviously also have to mark your ConnectionConfig as Serializable and add [XmlIgnore] to the _configuration field.
来源:https://stackoverflow.com/questions/5270895/problems-with-the-datacontractserialiser-and-serializable