일부 애플리케이션에서는 다양한 데이터 멤버의 데이터가 전송되거나 수신될 것으로 예상되는 순서(예: 데이터가 serialize된 XML에 표시되는 순서)를 아는 것이 유용합니다. 경우에 따라 이 순서를 변경해야 할 수 있습니다. 이 항목에서는 주문 규칙에 대해 설명합니다.
기본 규칙
데이터 순서 지정에 대한 기본 규칙은 다음과 같습니다.
데이터 계약 형식이 상속 계층 구조의 일부인 경우 기본 형식의 데이터 멤버는 항상 순서대로 우선합니다.
다음 순서로는 속성 집합의 Order 속성을 갖지 않은 현재 형식의 DataMemberAttribute 데이터 멤버가 사전순으로 표시됩니다.
다음은 Order 속성 집합에 속하는 DataMemberAttribute 속성을 가진 데이터 멤버입니다. 이러한 값은 먼저 속성 값에 따라 정렬된 다음, 특정
Order
값의Order
멤버가 두 개 이상 있는 경우 사전순으로 정렬됩니다. 주문 값이 생략될 수 있습니다.
메서드 CompareOrdinal을(를) 호출하여 사전순을 확립합니다.
예시
다음 코드를 고려합니다.
[DataContract]
public class BaseType
{
[DataMember]
public string zebra;
}
[DataContract]
public class DerivedType : BaseType
{
[DataMember(Order = 0)]
public string bird;
[DataMember(Order = 1)]
public string parrot;
[DataMember]
public string dog;
[DataMember(Order = 3)]
public string antelope;
[DataMember]
public string cat;
[DataMember(Order = 1)]
public string albatross;
}
<DataContract()> _
Public Class BaseType
<DataMember()> Public zebra As String
End Class
<DataContract()> _
Public Class DerivedType
Inherits BaseType
<DataMember(Order:=0)> Public bird As String
<DataMember(Order:=1)> Public parrot As String
<DataMember()> Public dog As String
<DataMember(Order:=3)> Public antelope As String
<DataMember()> Public cat As String
<DataMember(Order:=1)> Public albatross As String
End Class
생성된 XML은 다음과 유사합니다.
<DerivedType>
<!-- Zebra is a base data member, and appears first. -->
<zebra/>
<!-- Cat has no Order, appears alphabetically first. -->
<cat/>
<!-- Dog has no Order, appears alphabetically last. -->
<dog/>
<!-- Bird is the member with the smallest Order value -->
<bird/>
<!-- Albatross has the next Order value, alphabetically first. -->
<albatross/>
<!-- Parrot, with the next Order value, alphabetically last. -->
<parrot/>
<!-- Antelope is the member with the highest Order value. Note that
Order=2 is skipped -->
<antelope/>
</DerivedType>