Can an internal setter of a property be serialized?

前端 未结 6 948
盖世英雄少女心
盖世英雄少女心 2020-12-07 17:32

Is there any way to serialize a property with an internal setter in C#?
I understand that this might be problematic - but if there is a way - I would like to know.

6条回答
  •  粉色の甜心
    2020-12-07 17:57

    Not that I've found without doing some work. I believe this is because the XmlSerializer that is generated uses reflection to generate a new class (which is in a new assembly, so cannot see internal member/methods).

    There may be some mileage in using an XmlSerialization PreCompilier to generate the code, and then to modify it into an internal class for your purposes, so you'd then do something like:

    XmlSerializer serializer = new MyPersonXmlSerializer();
    

    The other option (and probably preferable) is to implement IXmlSerializable which will guide the auto-generated code to do the right thing.

提交回复
热议问题