EntLib Validation problem on GetType(object) - expects string not object?
I have an Address object that I am trying to validate data against using EntLib:
Given the following method:
<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
I create an address object:
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;
So now at cardX.billingAddress = thisAddress, the BillingAddress property validator (GetType(Address)) should fire. It seems to fire, but returns this error:
Value to validate is not of the expected type: expected System.String but got Address instead.
Can anyone see the issue here / suggest a fix?
Thanks.
I would just get rid of the ValidatorComposition
and TypeConversionValidator
declarations as I think they are redundant here. That would get rid of your error and a couple of lines of code too.
The property is already strongly-typed to the Address
class so there is no way you can set it in code to an object that isn't an Address
or isn't polymorphic with Address
- having an extra validator to check this is redundant.
The default composition of validators is logical AND anyway, you only need to specify validator composition when you want to OR a group, or use more complex groups that combine AND / OR.
精彩评论