クレジットカードの詳細を表すクラスがあります。有効な開始日と有効期限の月と年を表すために、 type の 4 つのプロパティを使用していますint
。
public int ValidFromMonth { get; set; }
public int ValidFromYear { get; set; }
public int ExpiresEndMonth { get; set; }
public int ExpiresEndYear { get; set; }
サードパーティが使用できるように、このクラスを XML シリアライズしています。そのサード パーティは、値が 10 未満の場合、月と年の値の前に 0 を付ける必要があります。
<validFromMonth>02</validFromMonth>
<validFromYear>09</validFromYear>
<expiresEndMonth>10</expiresEndMonth>
<expiresEndYear>14</expiresEndYear>
.NET は、おそらくフォーマット文字列 (例: ) を使用して、この規則を適用する属性をサポートしていますか (またはカスタム属性を作成することは可能{0:00}
ですか)?
注:string
内部で書式設定を行う独自のプロパティを追加[XmlIgnore]
し、プロパティに属性を追加できることはわかっていint
ますが、これは二流のソリューションのように感じます。
編集: いくつかの検討の後、これが実際に実現可能ではないかどうか疑問に思っています. シリアル化は問題ありませんが、逆シリアル化が機能するには、シリアル化された文字列のフォーマットを解除する必要があります。上記の些細な例では、これは簡単ですが、より一般的なケースで機能するようになるかどうかはわかりません。
Edit2: 2 桁の要件を定義する XML スキーマは以下のとおりです。
単純型定義:
<xs:simpleType name="CreditCardMonthType">
<xs:annotation>
<xs:documentation>Two digit month</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="2" />
<xs:maxLength value="2" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="CreditCardYearType">
<xs:annotation>
<xs:documentation>Two digit year</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="2" />
<xs:maxLength value="2" />
</xs:restriction>
</xs:simpleType>
これらのタイプを使用するクレジット カードの定義:
<xs:attribute name="ExpiryMonth" type="CreditCardMonthType" use="required">
<xs:annotation>
<xs:documentation>Credit/debt card's expiry month.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="ExpiryYear" type="CreditCardYearType" use="required">
<xs:annotation>
<xs:documentation>Credit/debt card's expiry year.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="StartMonth" type="CreditCardMonthType" use="optional">
<xs:annotation>
<xs:documentation>Switch card's start month.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="StartYear" type="CreditCardYearType" use="optional">
<xs:annotation>
<xs:documentation>Switch card's start year.</xs:documentation>
</xs:annotation>
</xs:attribute>