.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
CustomValiadtor in CustomConfigSection
CustomValiadtor in CustomConfigSection
- Hi,
I wrote a custom ConfigSection like this:Within the section I try to use a custom validator object the StringValueValidator that looks like this:Public Class LoaderConfigSection Inherits AConfigSectionBase Private Shared _section As LoaderConfigSection Private Shared _instantiationLock As New ReaderWriterLockSlim Private Sub New() End Sub Public Shared Function getSection() As LoaderConfigSection Try _instantiationLock.EnterWriteLock() If _section Is Nothing Then _section = CType(_config.GetSection("frameworkConfigGroup/loader"), LoaderConfigSection) If _section Is Nothing Then Throw New ConfigurationErrorsException("Section not found!!") End If End If Return _section Catch ex As Exception Throw Finally _instantiationLock.ExitWriteLock() End Try End Function ' <ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True), StringValueValidator("singleAppDomain;multipleAppdomains")> _ '<ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True)> _ <ConfigurationProperty("Mode", DefaultValue:="singleAppDomain", IsRequired:=True), StringValueValidator("singleAppDomain;multipleAppdomains")> _ Public Property Mode() As String Get Try _lock.EnterReadLock() Return (CType(Me("Mode"), String)) Catch ex As Exception Throw Finally _lock.ExitReadLock() End Try End Get Set(ByVal value As String) Try _lock.EnterWriteLock() Me("Mode") = value Catch ex As Exception Throw Finally _lock.ExitWriteLock() End Try End Set End Property End Class End Namespace
But everytime I start the app, an exception occurs: Error when creating the ConfigurationSectionHandler.The value must not be NULL. Parametername:Type.<AttributeUsage(AttributeTargets.All)> _ Public Class StringValueValidatorAttribute Inherits ConfigurationValidatorAttribute Private _validator As StringValueValidator Public Property ValidStrings() As String Get Return _validator.ValidStrings End Get Set(ByVal value As String) _validator.ValidStrings = value End Set End Property ''' <summary> ''' ''' </summary> ''' <param name="validStrings">the valid strings seperated by ;</param> ''' <remarks></remarks> Public Sub New(ByVal validStrings As String) _validator = New StringValueValidator(validStrings) End Sub Public Function CanValidate(ByVal type As System.Type) As Boolean Return _validator.CanValidate(type) End Function Public Sub Validate(ByVal value As Object) _validator.Validate(value) End Sub End Class
This exception only occurs, when I use the Stringvaluevalidator.
Thank you.
Harry
Answers
- Your string validator class must derive from ConfigurationValidatorBase. The CanValidate and Validate methods must be marked as overridable. You must also implement the ValidatorInstance property on your validator attribute. This property can return your created validator instance.
Michael Taylor - 11/5/09Public Class StringValueValidatorAttribute ... Public Overrides ReadOnly Property ValidatorInstance As ConfigurationValidatorBase Get Return _validator End Get End Property End Class Public Class StringValueValidator Inherits ConfigurationValidatorBase ... End Class
http://p3net.mvps.org
All Replies
- Your string validator class must derive from ConfigurationValidatorBase. The CanValidate and Validate methods must be marked as overridable. You must also implement the ValidatorInstance property on your validator attribute. This property can return your created validator instance.
Michael Taylor - 11/5/09Public Class StringValueValidatorAttribute ... Public Overrides ReadOnly Property ValidatorInstance As ConfigurationValidatorBase Get Return _validator End Get End Property End Class Public Class StringValueValidator Inherits ConfigurationValidatorBase ... End Class
http://p3net.mvps.org - Thanks a lot that worked.
- Thank you, Michael, for the sharing of the solution! :)
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


