【发布时间】: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