我有一个使用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填充的属性与用其他方法设置的属性不同。(反射、构造函数或属性设置器)。