代码之家  ›  专栏  ›  技术社区  ›  squig

序列化顺序与统一性问题

  •  2
  • squig  · 技术社区  · 16 年前

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

    现在,当我序列化这个类时,Unity设置的属性首先被序列化,然后依次是所有其他属性。

    因此,如果类属性2和3是由Unity设置的,那么序列化的类将以以下形式出现:23145

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

    代码示例 班

     [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>();
    

    我不想修改自定义Serlaiser,目前我刚刚将属性1添加到Unity配置中,以便进行一些测试。

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

    1 回复  |  直到 16 年前
        1
  •  1
  •   Community CDub    5 年前

    那么,您的自定义序列化程序如何选择顺序?你控制这个序列化程序吗?还是第三方?您描述的“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;}
    }
    

    Foo , Bar .

    (对于观察者来说:是的,上面的例子看起来很像与 Order