【问题标题】:Serialisation order & Unity problems序列化顺序和 Unity 问题
【发布时间】:2009-01-07 06:58:17
【问题描述】:

我有一个使用 XML 和自定义序列化程序的应用程序,并且最近使用 Unity 1.2 添加了一些 DI 以在我的类中设置两个属性。

现在,当我对类进行序列化时,Unity 设置的属性首先被序列化,然后是所有其他属性。

因此,如果 Unity 设置了类属性 2 和 3,则序列化类按以下顺序出现:23145

对于 XmlSerialiser,我可以使用 XmlElement 属性来设置顺序,但是我的自定义序列化器仍然以错误的顺序输出类,并且它的长度格式是固定的,因此顺序很重要。

有没有人知道在使用 Unity 的同时让我的课程按顺序序列化的方法?

谢谢

代码示例 类

 [Serializable]
[DataContract]
[XmlInclude(typeof(HeaderDocument))]
public class HeaderDocument
{
    [InjectionConstructor]
    public HeaderDocument()
    {
        DateTimeCreated  = DateTime.Now;
        TransactionId  = Guid.NewGuid().ToString();
        HasPayload = true;
    }

    /// <summary>Gets or sets the service name of the target service.</summary>
    [DataMember, CopyBookElement("REQUESTED-SERVICE", CopyBookDataType.String, Length = 40)]
    public string ServiceName { get; set; } 

    /// <summary>Gets or sets the entry application name of the request.</summary>
    [DataMember, CopyBookElement("CONSUMER-APPLICATION-ID", CopyBookDataType.UnsignedInteger, Length = 3)]
    public int ApplicationId { get; set; } 

    /// <summary>Gets or sets the quality of service required for the request.</summary>
    [DataMember, CopyBookElement("QUALITY-OF-SERVICE", CopyBookDataType.String, Length = 1)]
    public string QualityOfService { get; set; }

    /// <summary>Gets or sets the transaction identifier.</summary>
    [DataMember, CopyBookElement("TRANSACTION-ID", CopyBookDataType.String, Length = 36)]
    public string TransactionId { get; set; }

    /// <summary>Gets or sets the time the document was created.</summary>
    public DateTime DateTimeCreated { get; set; }

    [DataMember, CopyBookElement("HAS-PAYLOAD", CopyBookDataType.Boolean)]
    public bool HasPayload { get; set; }
}

统一配置

<unity>
<typeAliases />
<containers>
  <container name="TechnicalArchitecture">
    <types>
      <type type="ILogger, ApplicationServices" mapTo="Logger, ApplicationServices" />
      <type type="HeaderDocument, ApplicationServices">
        <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
          <property name="ApplicationId" propertyType="System.Int32">
            <value value="19" />
          </property>
          <property name="QualityOfService" propertyType="System.String">
            <value value="1" />
          </property>
        </typeConfig>
      </type>
    </types>
  </container>
</containers>

在构造 HeaderDocument 时,我调用了

HeaderDocument = IOC.Container.Resolve<HeaderDocument>();

我不希望修改自定义序列化器,目前我刚刚将属性 1 添加到 Unity 配置中,因此我可以进行一些测试。

但我真正想知道的是,为什么 Unity 填充的属性与任何其他方法设置的属性的处理方式不同。 (反射、构造函数或属性设置器)。

【问题讨论】:

  • 嗨,代码示例/sn-p 有机会吗?我认为您设置的属性是简单类型 - 例如字符串或数字 - 不是对其他对象的引用......

标签: .net serialization unity-container


【解决方案1】:

那么,您的自定义序列化程序如何选择顺序?你控制这个序列化器吗?还是第三者?您描述的“2、3、1、4、5”模式让我们怀疑您的对象是否不是“属性包”,它只是按照设置的顺序序列化数据 - 这将是 高度 有风险。您设置属性的顺序应该(通常)不重要。

属性没有定义的顺序,您不应依赖构建之间的此顺序。系统也不能保证反射、源代码和任何东西的属性顺序。

所以你需要自己定义顺序,也许是按字母顺序(但是当有人添加 AardvarkCount 属性时,这很脆弱),也许是通过自定义属性。

我有一个custom (binary) serializer,我使用自定义属性方法,尽管在某些情况下我支持字母顺序。例如:

[ProtoContract]
public class MyClass {
    [ProtoMember(1)]
    public int Foo {get;set;}

    [ProtoMember(2)]
    public string Bar {get;set;}
}

那么反射以什么顺序排列属性并不重要 - 序列化程序总是按照FooBar 的顺序读取/写入数据。

(对于细心的人:是的,上面的示例看起来很像带有Order 属性集的 WCF / 数据合同,所以我也支持它;-p)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2021-04-13
    • 2022-01-20
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    相关资源
    最近更新 更多