【问题标题】:EntLib Validation problem on GetType(object) - expects string not object?GetType(object) 上的 EntLib 验证问题 - 期望字符串不是对象?
【发布时间】:2010-02-04 01:21:30
【问题描述】:

我有一个地址对象,我正在尝试使用 EntLib 验证数据:

给定以下方法:

<ValidatorComposition(CompositionType.And, Ruleset:="FraudAnalysis")> _
<NotNullValidator(MessageTemplate:="Billing address is required.", Ruleset:="FraudAnalysis")> _
<TypeConversionValidator(GetType(Address), MessageTemplate:="Billing address must be an address object.", Ruleset:="FraudAnalysis")> _
Public Property BillingAddress() As Address
    Get
        Return _BillingAddress
    End Get
    Set(ByVal value As Address)
        _BillingAddress = value
    End Set
End Property

我创建一个地址对象:

            Address thisAddress = new Address();
            thisAddress.Address1 = "12312 Long Street";
            thisAddress.City = "Los Angeles";
            thisAddress.State = "CA";
            thisAddress.Zip = "93322";
            // set billing address to address
            cardX.BillingAddress = thisAddress;

所以现在在 cardX.billingAddress = thisAddress 处,BillingAddress 属性验证器 (GetType(Address)) 应该触发。它似乎触发了,但返回此错误:

要验证的值不是预期的类型:预期的 System.String 但得到了 Address。

任何人都可以在这里看到问题/建议修复吗?

谢谢。

【问题讨论】:

    标签: validation enterprise-library gettype


    【解决方案1】:

    我会删除 ValidatorCompositionTypeConversionValidator 声明,因为我认为它们在这里是多余的。这将消除您的错误和几行代码。

    该属性已被强类型化为 Address 类,因此您无法在代码中将其设置为不是 Address 或与 Address 不具有多态性的对象 - 具有额外的验证器来检查这是多余的。

    验证器的默认组合无论如何都是逻辑与,您只需要在想要对组进行 OR 或使用组合 AND / OR 的更复杂的组时指定验证器组合。

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2015-06-27
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多