C# 如何在C中使用自定义序列化程序序列化容器类#

C# 如何在C中使用自定义序列化程序序列化容器类#,c#,serialization,C#,Serialization,我需要序列化一些容器类,HasValue属性的计算结果为true 在序列化之前,我不需要从容器列表中删除无效元素。序列化程序应该能够确定哪些对象需要序列化或不需要序列化。我想自定义序列化程序可以满足我的需要,但我不知道如何解决这个问题。如有任何其他解决方案/最佳做法,将不胜感激 这是我的课 public static class ContainerFactory { public static Container Create() {

我需要序列化一些容器类,
HasValue
属性的计算结果为true

在序列化之前,我不需要从容器列表中删除无效元素。序列化程序应该能够确定哪些对象需要序列化或不需要序列化。我想自定义序列化程序可以满足我的需要,但我不知道如何解决这个问题。如有任何其他解决方案/最佳做法,将不胜感激

这是我的课

public static class ContainerFactory
{
         public static Container Create()
         {
             var container = new Container();
             container.Persons.AddRange(new[]
                 {
                     new Person
                         {
                             FirstName = "Thomas"
                         },
                      new Person
                         {
                             FirstName = "Andrew",
                             LastName = "Martin",
                              Vehicles = new Vehicles
                                 {
                                    new Vehicle { Hsn = "65976GHR", Tsn = "HUZUKL"}
                                 }
                         },
                          new Person
                         {
                             FirstName = "Arnold",
                             LastName = "Beckmann",
                             Vehicles = new Vehicles
                                 {
                                    new Vehicle { Hsn = "345XXXHZ"},
                                    new Vehicle { Hsn = "659JUKI", Tsn = "787999HGF"}
                                 }
                         }

                 });
             return container;
         }
}

[Serializable]
public class Container
{
    public Container()
    {
        Persons = new Persons();
    }

    public Persons Persons { get; set; }

    public void Serialize()
    {
        var serializer = new XmlSerializer(typeof (Container));
        var streamWriter = new StreamWriter(@"C:\container.xml", false);
        serializer.Serialize(streamWriter, this);
    }
}

public class Persons: List<Person>
{
}

public class Vehicles: List<Vehicle>
{
    public Vehicles()
    {
    }

    public Vehicles(IEnumerable<Vehicle> vehicles):base(vehicles)
    {
    }
}

[Serializable]
public class Person : IHasValue
{
    public Person()
    {
        this.Vehicles = new Vehicles();
        this.Id = Guid.NewGuid().ToString();
    }

    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Vehicles Vehicles { get; set; }

    public bool HasValue
    {
        get { return !string.IsNullOrEmpty(this.FirstName) && !string.IsNullOrEmpty(this.LastName); }
    }
}

public interface IHasValue
{
    bool HasValue { get;}
}

public class Vehicle: IHasValue
{
    public string Hsn { get; set; }
    public string Tsn { get; set; }

    public bool HasValue
    {
        get { return !string.IsNullOrEmpty(Hsn) && !string.IsNullOrEmpty(Tsn); }
    }
}

//Using the .NET XMLSerializer to test my container
Container container = ContainerFactory.Create();
container.Serialize();
Console.WriteLine("Press any Key to continue...");
Console.ReadLine(); 
公共静态类ContainerFactory
{
公共静态容器Create()
{
var container=新容器();
container.Persons.AddRange(新[]
{
新人
{
FirstName=“托马斯”
},
新人
{
FirstName=“安德鲁”,
LastName=“Martin”,
车辆=新车
{
新车{Hsn=“65976GHR”,Tsn=“HUZUKL”}
}
},
新人
{
FirstName=“Arnold”,
LastName=“贝克曼”,
车辆=新车
{
新车{Hsn=“345XXXHZ”},
新车{Hsn=“659JUKI”,Tsn=“787999HGF”}
}
}
});
返回容器;
}
}
[可序列化]
公营货柜
{
公共容器()
{
人员=新人员();
}
公众人物{get;set;}
public void Serialize()
{
var serializer=新的XmlSerializer(typeof(Container));
var streamWriter=newstreamwriter(@“C:\container.xml”,false);
serializer.Serialize(streamWriter,this);
}
}
公众人士:名单
{
}
公营车辆:名单
{
公共车辆()
{
}
公共车辆(IEnumerable Vehicles):基本车辆(Vehicles)
{
}
}
[可序列化]
公共类人士:IHasValue
{
公众人士()
{
此项。车辆=新车();
this.Id=Guid.NewGuid().ToString();
}
公共字符串Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共车辆{get;set;}
公共布尔值
{
获取{return!string.IsNullOrEmpty(this.FirstName)&&!string.IsNullOrEmpty(this.LastName);}
}
}
公共接口IHasValue
{
bool HasValue{get;}
}
公共级车辆:IHasValue
{
公共字符串Hsn{get;set;}
公共字符串Tsn{get;set;}
公共布尔值
{
获取{return!string.IsNullOrEmpty(Hsn)&!string.IsNullOrEmpty(Tsn);}
}
}
//使用.NET XMLSerializer测试我的容器
Container=ContainerFactory.Create();
container.Serialize();
Console.WriteLine(“按任意键继续…”);
Console.ReadLine();
输出

<?xml version="1.0" encoding="utf-8"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Persons>
    <Person>
      <Id>76cdcc18-b256-40fe-b813-cd6c60e682ca</Id>
      <FirstName>Thomas</FirstName>
      <Vehicles />
    </Person>
    <Person>
      <Id>26623bf9-d799-44d2-bc1a-7ec91292d1cd</Id>
      <FirstName>Andrew</FirstName>
      <LastName>Martin</LastName>
      <Vehicles>
        <Vehicle>
          <Hsn>65976GHR</Hsn>
          <Tsn>HUZUKL</Tsn>
        </Vehicle>
      </Vehicles>
    </Person>
    <Person>
      <Id>f645cde1-10c8-4df5-81df-9b9db7712ec3</Id>
      <FirstName>Arnold</FirstName>
      <LastName>Beckmann</LastName>
      <Vehicles>
        <Vehicle>
          <Hsn>345XXXHZ</Hsn>
        </Vehicle>
        <Vehicle>
          <Hsn>659JUKI</Hsn>
          <Tsn>787999HGF</Tsn>
        </Vehicle>
      </Vehicles>
    </Person>
  </Persons>

76cdcc18-b256-40fe-b813-cd6c60e682ca
托马斯
26623bf9-d799-44d2-bc1a-7ec91292d1cd
安得烈
马丁
65976GHR
胡祖克
f645cde1-10c8-4df5-81df-9b9db7712ec3
阿诺德
贝克曼
345XXXHZ
659JUKI
787999HGF
如何实现仅对
HasValue==true
的车辆/人员进行序列化的目标?

您需要使用:

  • -要序列化的每个类以及要在不同节点中使用的每个属性都需要使用适当的属性。看看这个例子
  • 序列化类时,请检查
    HasValue==true

  • 人员:列表也应可序列化

    public void GetObjectData( SerializationInfo info, StreamingContext context )    
    {
        foreach (Person person in this)    
        {
            if(person.HasValue)
            {
                info.AddValue("Firsname", person, typeof(Person));
    
                info.AddValue (....);
    
                ..............
    
            }
        }
    }