I m 寻求一种办法,将含有一些只读的特性的POCO序列化。 在一些谷歌和StackOverflow搜索中,我看到了以下建议:
- use DataContractSerializer; or
- use SoapFormatter or BinaryFormatter; or
- replace my readonly properties by read/write properties;
我的班子非常简单,他们喜欢:
public class MyClass
{
public int Id { get; private set; }
public string Name { get; private set; }
public MyClass(int id, string name)
{
Id = id;
Name = name;
}
}
因此,
- I don t want to make my properties read/write. If they are read-only, it s because my domain model asks for read-only properties. The domain model cannot change just because of this.
- I don t want to use
DataContractSerializer
, as this would pollute my domain model with serialization-related stuff. BinaryFormatter
is not a very good option, as the result is abyte[]
, and I would like to treat it asstring
(and I don t want to deal with Encondings and alike when Deserializing my object).
我真的喜欢的是。 能够编篡只读物的XmlSerializer类别。
你们是否知道这种执行? 或任何其他方便的解决办法?
感谢!