1

我正在使用 xsd2code 从模式生成类。我怎么怀疑自动属性是否可以验证值?

因为如果我AutomaticProperties在 xsd2code 中启用,这就是我为具有正则表达式限制的成员所得到的。

[System.Xml.Serialization.XmlAttributeAttribute(DataType="token", AttributeName="color")]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute("#[\\dA-F]{6}([\\dA-F][\\dA-F])?")]
public string Color { get; set; }

什么时候AutomaticProperties被禁用

[System.Xml.Serialization.XmlAttributeAttribute(DataType="token", AttributeName="color")]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute("#[\\dA-F]{6}([\\dA-F][\\dA-F])?")]
public string Color
{
    get
    {
        return this._color;
    }
    set
    {
        System.ComponentModel.DataAnnotations.ValidationContext validatorPropContext = new System.ComponentModel.DataAnnotations.ValidationContext(this, null, null);
        validatorPropContext.MemberName = "Color";
        Validator.ValidateProperty(value, validatorPropContext);
        this._color = value;
    }
}

看来这些不是等价的。所以我认为它是 xsd2code 中的一个错误,或者我可能误解了一些东西。第二个生成代码的目的是什么?

我认为RegularExpressionAttribute也会验证自动属性。

4

0 回答 0