序列化到XML - 私有属性 [英] Serialize to XML - private properties

查看:222
本文介绍了序列化到XML - 私有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式来序列化,包含一些只读属性POCO。在有些谷歌和计算器搜索,我已经看到了以下的建议:

I'm looking for a way to serialize a POCO that contains some read-only properties. In some Google and StackOverflow searches, I've seen the following suggestions:

  • 使用的DataContractSerializer ;或
  • 使用的 SoapFormatter 的BinaryFormatter ;或
  • 将取代我只读性质的读/写性能
  • use DataContractSerializer; or
  • use SoapFormatter or BinaryFormatter; or
  • replace my readonly properties by read/write properties;

我的类很简单,他们是这样的:

My classes are very simple, they look like:

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;
    }
}

因此​​,

  • 我不想让我的属性读/写。如果他们是只读的,那是因为我的域模型要求只读属性。领域模型不能改变正因为如此。
  • 我不想使用的DataContractSerializer ,因为这会污染我与序列化相关的东西的领域模型。
  • 的BinaryFormatter 是不是一个很好的选择,因为其结果是字节[] ,我想把它当作字符串(我不希望我的反序列化对象时,应对Encondings和一致好评)。
  • 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 a byte[], and I would like to treat it as string (and I don't want to deal with Encondings and alike when Deserializing my object).

我真希望是 XmlSerializer的可序列化的只读属性的类

你知道任何这样的实现?或任何其他方便的解决方案?

Do you know of any such implementation? Or any other convenient solution?

谢谢!

推荐答案

哦,通常的XmlSerializer 不能......但有一种可能性序列只读属性序列化的属性与一组内部:你需要生成XML序列化程序集,并宣布它作为一个朋友组件,使用 InternalsVisibleTo 属性。您可以通过添加以下code到您的项目文件中自动完成:

Well, normally XmlSerializer can't serialize read-only properties... however there is a possibility to serialize properties with an internal set : you need to generate the XML serialization assembly, and declare it as a "friend" assembly using the InternalsVisibleTo attribute. You can automate this by adding the following code to your project file :

  <Target Name="AfterBuild"
          DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
          Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
          Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)"
          BuildAssemblyPath="$(OutputPath)"
          References="@(ReferencePath)"
          ShouldGenerateSerializer="true"
          UseProxyTypes="false"
          KeyContainer="$(KeyContainerName)"
          KeyFile="$(KeyOriginatorFile)"
          DelaySign="$(DelaySign)"
          ToolPath="$(SGenToolPath)">
      <Output TaskParameter="SerializationAssembly"
              ItemName="SerializationAssembly" />
    </SGen>
  </Target>

而在AssemblyInfo.cs中:

And in AssemblyInfo.cs :

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

当然,你可能不想要的属性,以拥有一组内部,但如果这样做,上述解决方案应该工作。

Of course, you might not want the properties to have an internal set, but if you do, the solution above should work.

这篇关于序列化到XML - 私有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆