我已使用Enterprise Library Configuration Console正确配置了验证块。
我的app.config文件如下所示(我只在这里显示验证部分):
<validation>
<type name="XFRop.Settings" defaultRuleset="All Settings Validation Ruleset"
assemblyName="XFRop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<ruleset name="All Settings Validation Ruleset">
<properties>
<property name="RemoteIPAddress">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
pattern="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
tag="RemoteIpAddress_Validator" name="Regular Expression Validator" />
</property>
<property name="RemotePort">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
culture="es-ES" lowerBound="0" lowerBoundType="Inclusive"
upperBound="65535" tag="RemotePort_Range_Validator" name="Range Validator" />
</property>
<property name="Culture">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
pattern="^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$" tag="Culture_Validator"
name="Regular Expression Validator" />
</property>
</properties>
</ruleset>
</type>
</validation>
我在VB.NET中有以下类(我将在这里将我的类简化为三个属性):
Public NotInheritable Class Settings
Private Shared _RemoteIPAddress As String
Private Shared _RemotePort As UShort
Private Shared _Culture As String
Public Shared Property RemoteIPAddress() As String
Get
Return _RemoteIPAddress
End Get
Set(ByVal value As String)
_RemoteIPAddress = value
End Set
End Property
Public Shared Property RemotePort() As UShort
Get
Return _RemotePort
End Get
Set(ByVal value As UShort)
_RemotePort = value
End Set
End Property
Public Shared Property Culture() As String
Get
Return _Culture
End Get
Set(ByVal value As String)
_Culture = value
End Set
End Property
End Class
请注意:
RemoteIPAddress是一个IPv4地址,因此我构建了以下常规 表达式来检查它:
\ B(25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9])(25 [0-5]。| 2 [0-4] [0-9] | [01] [0-9] [0-9])(25 [0-5] |?2 [0-4] [0-9] | [ 01] [0-9] [0-9])(25 [0-5] |?2 [0-4] [0-9] | [01] [0-9] [0-9] ?)\ b'/ p>
RemotePort是一个包含端口范围为0-65535的属性。
Culture是一个包含语言文化名称的属性,因此我构建了正则表达式:
^ [A-ZA-Z] {1,8}( - [A-ZA-Z0-9] {1,8})* $
现在,当我的应用程序启动时,我按如下所示初始化我的设置类:
Private Sub InitSettings()
Settings.RemoteIPAddress = My.Settings.RemoteIPAddress
Settings.RemotePort = My.Settings.RemotePort
Settings.Culture = My.Settings.Culture
End Sub
然后我在&#34;设置&#34;中验证设置。使用以下验证码进行分类:
Public Sub Main()
' Initialize application settings
Me.InitSettings()
' Validate current application settings
results = Validation.Validate(New Settings)
If results.IsValid Then
' Initiate Background worker thread
Me.DoTask()
End If
End Sub
验证有两个问题:
验证返回true(results.IsValid = true)但RemoteIPAddress属性包含空字符串。如果RemoteIPAddress不抱怨正则表达式,为什么验证返回true?它是一个空字符串,因此它应该返回false而不是true。注意:我已经从控制台配置了正则表达式验证器,并将Negated属性设置为默认值:false。
结果对象包含以下信息:
似乎验证没有正确应用,所以我做错了什么?