C# 从列表派生的类的序列化<&燃气轮机;使用数据契约
我正在尝试使用DataContract序列化从List派生的类。问题是我的类的属性不会被序列化 我的班级:C# 从列表派生的类的序列化<&燃气轮机;使用数据契约,c#,serialization,datacontract,C#,Serialization,Datacontract,我正在尝试使用DataContract序列化从List派生的类。问题是我的类的属性不会被序列化 我的班级: [CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/ public class DownloadRuleCollection : List<DownloadRule> { [DataMember(EmitDefaultValue = false)] public string SomePr
[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : List<DownloadRule> {
[DataMember(EmitDefaultValue = false)]
public string SomeProperty { get; set; }
//this is, in fact, more complex, but this is enough for the example
}
[DataContract]
public class DownloadRule {
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
/*
more properties
...
*/
}
结果:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDownloadRule xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1">
<DownloadRule>
<Name>test01</Name>
</DownloadRule>
<DownloadRule>
<Name>test02</Name>
</DownloadRule>
<DownloadRule>
<Name>test03</Name>
</DownloadRule>
</ArrayOfDownloadRule>
测试01
测试02
测试03
如您所见,列表的项被正确序列化(和反序列化),但列表本身没有被序列化。我尝试使用不同的属性:[可序列化]
,无更改[DataContract]
,在序列化期间引发异常(集合不能使用此属性)
顺便说一句,我也在序列化私有字段,所以我不能使用
XmlSerializer
(或其他不能序列化私有字段的类)。使用IList。应该正确序列化
[CollectionDataContract] /*[Serializable]*/ /*[DataContract]*/
public class DownloadRuleCollection : IList<DownloadRule> {
好的,爬山者104的解决方案是可行的,但我需要重新实现所有的
列表方法,这让我觉得我正在重新发明轮子
Jarek Waliszko线程中的JaredPar建议使用包装器类。
最简单的方法是为了序列化过程而使用它,所以我使用了一个受保护的Inner包装类。这使得我只需要几行代码就可以实现我的目标
public class DownloadRuleCollection : List<DownloadRule> {
public string SomeProperty { get; set; }
public void Serialize(string fileName) {
Serializer.Serialize(
new DownloadRuleCollection_SerializationWrapper {
Collection = this,
SomeProperty = SomeProperty
}, fileName);
}
public static DownloadRuleCollection Deserialize(string fileName) {
var wrapper = Serializer.Deserialize<DownloadRuleCollection_SerializationWrapper>(fileName);
var result = wrapper.Collection;
result.SomeProperty = wrapper.SomeProperty;
return result;
}
[DataContract(Name = "DownloadRuleCollection")]
private class DownloadRuleCollection_SerializationWrapper {
[DataMember(EmitDefaultValue = false, Name = "SomeProperty", Order = 0)]
public string SomeProperty { get; set; }
[DataMember(EmitDefaultValue = false, Name = "DownloadRules", Order = 1)]
public DownloadRuleCollection Collection;
}
}
[DataContract]
public class DownloadRule {
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
}
public static class Serializer {
public static void Serialize<T>(T obj, string fileName) {
using(XmlWriter writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true }))
new DataContractSerializer(typeof(T)).WriteObject(writer, obj);
}
public static T Deserialize<T>(Stream stream) {
return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
}
public static T Deserialize<T>(string fileName) {
using(FileStream fs = File.OpenRead(fileName)) {
return Deserialize<T>(fs);
}
}
}
公共类下载规则集合:列表{
公共字符串SomeProperty{get;set;}
公共void序列化(字符串文件名){
序列化程序(
新的DownloadRuleCollection\u SerializationWrapper{
集合=这个,
SomeProperty=SomeProperty
},文件名);
}
公共静态下载RuleCollection反序列化(字符串文件名){
var wrapper=Serializer.Deserialize(文件名);
var result=wrapper.Collection;
result.SomeProperty=wrapper.SomeProperty;
返回结果;
}
[DataContract(Name=“DownloadRuleCollection”)]
私有类下载RuleCollection\u SerializationWrapper{
[DataMember(EmitDefaultValue=false,Name=“SomeProperty”,Order=0)]
公共字符串SomeProperty{get;set;}
[DataMember(EmitDefaultValue=false,Name=“DownloadRules”,Order=1)]
公共下载规则集合;
}
}
[数据合同]
公共类下载规则{
[数据成员(EmitDefaultValue=false)]
公共字符串名称{get;set;}
}
公共静态类序列化程序{
公共静态void序列化(T obj,字符串文件名){
使用(XmlWriter=XmlWriter.Create(文件名,新的XmlWriterSettings{Indent=true}))
新的DataContractSerializer(typeof(T)).WriteObject(writer,obj);
}
公共静态T反序列化(流){
返回(T)新的DataContractSerializer(typeof(T)).ReadObject(stream);
}
公共静态T反序列化(字符串文件名){
使用(FileStream fs=File.OpenRead(文件名)){
返回反序列化(fs);
}
}
}
不,这不是我需要的。序列化DownloadRule
类没有问题,序列化DownloadRuleCollection
本身有问题,尤其是它的属性。
[DataContract(Namespace="http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
public class Message {
public Message() {
InsuranceList = new List<Insurance>();
MessageId = GuidComb.NewGuid();
}
[IgnoreDataMember]
public Guid MessageId { get; private set; }
#region "Data"
[DataMember]
public string MessageTypeIndicator { get; set; }
[DataMember]
public MessageConfiguration MessageConfiguration { get; set; }
[DataMember]
public Patient Patient { get; set; }
[DataMember]
public Encounter Encounter { get; set; }
[DataMember]
public IList<Insurance> InsuranceList { get; set; }
#endregion
[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/InboundIntegration.HL7Messaging")]
public class Insurance {
[DataMember]
public string ExternalPayerId { get; set; }
[DataMember]
public string PayerName { get; set; }
[DataMember]
public string GroupNumber { get; set; }
[DataMember]
public string MemberIdOfPatient { get; set; }
[DataMember]
public string PatientRelationshipToInsuredIndicator { get; set; }
[DataMember]
public string CoordinationOfBenefitsPrecedenceIndicator { get; set; }
public class DownloadRuleCollection : List<DownloadRule> {
public string SomeProperty { get; set; }
public void Serialize(string fileName) {
Serializer.Serialize(
new DownloadRuleCollection_SerializationWrapper {
Collection = this,
SomeProperty = SomeProperty
}, fileName);
}
public static DownloadRuleCollection Deserialize(string fileName) {
var wrapper = Serializer.Deserialize<DownloadRuleCollection_SerializationWrapper>(fileName);
var result = wrapper.Collection;
result.SomeProperty = wrapper.SomeProperty;
return result;
}
[DataContract(Name = "DownloadRuleCollection")]
private class DownloadRuleCollection_SerializationWrapper {
[DataMember(EmitDefaultValue = false, Name = "SomeProperty", Order = 0)]
public string SomeProperty { get; set; }
[DataMember(EmitDefaultValue = false, Name = "DownloadRules", Order = 1)]
public DownloadRuleCollection Collection;
}
}
[DataContract]
public class DownloadRule {
[DataMember(EmitDefaultValue = false)]
public string Name { get; set; }
}
public static class Serializer {
public static void Serialize<T>(T obj, string fileName) {
using(XmlWriter writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true }))
new DataContractSerializer(typeof(T)).WriteObject(writer, obj);
}
public static T Deserialize<T>(Stream stream) {
return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
}
public static T Deserialize<T>(string fileName) {
using(FileStream fs = File.OpenRead(fileName)) {
return Deserialize<T>(fs);
}
}
}