Using XmlSerializer with private and public const properties

前端 未结 6 2073
渐次进展
渐次进展 2020-12-19 08:07

What\'s the simplest way to get XmlSerializer to also serialize private and \"public const\" properties of a class or struct? Right not all it will output for me is things

6条回答
  •  鱼传尺愫
    2020-12-19 09:14

    Even though it's not possible to serialize private properties, you can serialize properties with an internal setter, like this one :

    public string Foo { get; internal set; }
    

    To do that, you need to pre-generate the serialization assembly with sgen.exe, and declare this assembly as friend :

    [assembly:InternalsVisibleTo("MyAssembly.XmlSerializers")]
    

提交回复
热议问题