ConfigurationConverterBase Class
The base class for the configuration converter types.
Assembly: System.Configuration (in System.Configuration.dll)
System.ComponentModel.TypeConverter
System.Configuration.ConfigurationConverterBase
System.Configuration.CommaDelimitedStringCollectionConverter
System.Configuration.GenericEnumConverter
System.Configuration.InfiniteIntConverter
System.Configuration.InfiniteTimeSpanConverter
System.Configuration.TimeSpanMinutesConverter
System.Configuration.TimeSpanSecondsConverter
System.Configuration.TypeNameConverter
System.Configuration.WhiteSpaceTrimStringConverter
System.Web.Configuration.MachineKeyValidationConverter
System.Web.UI.MobileControls.MobileTypeNameConverter
| Name | Description | |
|---|---|---|
![]() | ConfigurationConverterBase() | Initializes a new instance of the ConfigurationConverterBase class. |
| Name | Description | |
|---|---|---|
![]() | CanConvertFrom(ITypeDescriptorContext, Type) | Determines whether the conversion is allowed.(Overrides TypeConverter.CanConvertFrom(ITypeDescriptorContext, Type).) |
![]() | CanConvertFrom(Type) | Returns whether this converter can convert an object of the given type to the type of this converter.(Inherited from TypeConverter.) |
![]() | CanConvertTo(ITypeDescriptorContext, Type) | Determines whether the conversion is allowed.(Overrides TypeConverter.CanConvertTo(ITypeDescriptorContext, Type).) |
![]() | CanConvertTo(Type) | Returns whether this converter can convert the object to the specified type.(Inherited from TypeConverter.) |
![]() | ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) | Converts the given object to the type of this converter, using the specified context and culture information.(Inherited from TypeConverter.) |
![]() | ConvertFrom(Object) | Converts the given value to the type of this converter.(Inherited from TypeConverter.) |
![]() | ConvertFromInvariantString(ITypeDescriptorContext, String) | Converts the given string to the type of this converter, using the invariant culture and the specified context.(Inherited from TypeConverter.) |
![]() | ConvertFromInvariantString(String) | Converts the given string to the type of this converter, using the invariant culture.(Inherited from TypeConverter.) |
![]() | ConvertFromString(ITypeDescriptorContext, CultureInfo, String) | Converts the given text to an object, using the specified context and culture information.(Inherited from TypeConverter.) |
![]() | ConvertFromString(ITypeDescriptorContext, String) | Converts the given text to an object, using the specified context.(Inherited from TypeConverter.) |
![]() | ConvertFromString(String) | Converts the specified text to an object.(Inherited from TypeConverter.) |
![]() | ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) | Converts the given value object to the specified type, using the specified context and culture information.(Inherited from TypeConverter.) |
![]() | ConvertTo(Object, Type) | Converts the given value object to the specified type, using the arguments.(Inherited from TypeConverter.) |
![]() | ConvertToInvariantString(ITypeDescriptorContext, Object) | Converts the specified value to a culture-invariant string representation, using the specified context.(Inherited from TypeConverter.) |
![]() | ConvertToInvariantString(Object) | Converts the specified value to a culture-invariant string representation.(Inherited from TypeConverter.) |
![]() | ConvertToString(ITypeDescriptorContext, CultureInfo, Object) | Converts the given value to a string representation, using the specified context and culture information.(Inherited from TypeConverter.) |
![]() | ConvertToString(ITypeDescriptorContext, Object) | Converts the given value to a string representation, using the given context.(Inherited from TypeConverter.) |
![]() | ConvertToString(Object) | Converts the specified value to a string representation.(Inherited from TypeConverter.) |
![]() | CreateInstance(IDictionary) | Re-creates an Object given a set of property values for the object.(Inherited from TypeConverter.) |
![]() | CreateInstance(ITypeDescriptorContext, IDictionary) | Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.(Inherited from TypeConverter.) |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetConvertFromException(Object) | Returns an exception to throw when a conversion cannot be performed.(Inherited from TypeConverter.) |
![]() | GetConvertToException(Object, Type) | Returns an exception to throw when a conversion cannot be performed.(Inherited from TypeConverter.) |
![]() | GetCreateInstanceSupported() | Returns whether changing a value on this object requires a call to the CreateInstance method to create a new value.(Inherited from TypeConverter.) |
![]() | GetCreateInstanceSupported(ITypeDescriptorContext) | Returns whether changing a value on this object requires a call to CreateInstance to create a new value, using the specified context.(Inherited from TypeConverter.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetProperties(ITypeDescriptorContext, Object) | Returns a collection of properties for the type of array specified by the value parameter, using the specified context.(Inherited from TypeConverter.) |
![]() | GetProperties(ITypeDescriptorContext, Object, Attribute()) | Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.(Inherited from TypeConverter.) |
![]() | GetProperties(Object) | Returns a collection of properties for the type of array specified by the value parameter.(Inherited from TypeConverter.) |
![]() | GetPropertiesSupported() | Returns whether this object supports properties.(Inherited from TypeConverter.) |
![]() | GetPropertiesSupported(ITypeDescriptorContext) | Returns whether this object supports properties, using the specified context.(Inherited from TypeConverter.) |
![]() | GetStandardValues() | Returns a collection of standard values from the default context for the data type this type converter is designed for.(Inherited from TypeConverter.) |
![]() | GetStandardValues(ITypeDescriptorContext) | Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.(Inherited from TypeConverter.) |
![]() | GetStandardValuesExclusive() | Returns whether the collection of standard values returned from GetStandardValues is an exclusive list.(Inherited from TypeConverter.) |
![]() | GetStandardValuesExclusive(ITypeDescriptorContext) | Returns whether the collection of standard values returned from GetStandardValues is an exclusive list of possible values, using the specified context.(Inherited from TypeConverter.) |
![]() | GetStandardValuesSupported() | Returns whether this object supports a standard set of values that can be picked from a list.(Inherited from TypeConverter.) |
![]() | GetStandardValuesSupported(ITypeDescriptorContext) | Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.(Inherited from TypeConverter.) |
![]() | GetType() | |
![]() | IsValid(ITypeDescriptorContext, Object) | Returns whether the given value object is valid for this type and for the specified context.(Inherited from TypeConverter.) |
![]() | IsValid(Object) | Returns whether the given value object is valid for this type.(Inherited from TypeConverter.) |
![]() | MemberwiseClone() | |
![]() | SortProperties(PropertyDescriptorCollection, String()) | Sorts a collection of properties.(Inherited from TypeConverter.) |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
The ConfigurationConverterBase is the base class for the converter configuration types. These are types that convert strings, found in the configuration file, to and from the related strongly typed properties.
The following code examples show how to derive from the ConfigurationConverterBase class to create a custom TimeSpan converter type. Also, the examples show how to use this type in a custom section.
The following code example shows how to create a custom TimeSpan converter type from the ConfigurationConverterBase class.
Imports System Imports System.Collections.Generic Imports System.Text Imports System.Configuration Imports System.Globalization Imports System.ComponentModel NotInheritable Public Class CustomTimeSpanMinutesConverter Inherits ConfigurationConverterBase Friend Function ValidateType(ByVal value As Object, _ ByVal expected As Type) As Boolean Dim result As Boolean If Not (value Is Nothing) _ AndAlso value.ToString() <> expected.ToString() Then result = False Else result = True End If Return result End Function 'ValidateType Public Overrides Function CanConvertTo( _ ByVal ctx As ITypeDescriptorContext, _ ByVal type As Type) As Boolean Return (type.ToString() = GetType(String).ToString()) End Function 'CanConvertTo Public Overrides Function CanConvertFrom( _ ByVal ctx As ITypeDescriptorContext, _ ByVal type As Type) As Boolean Return (type.ToString() = GetType(String).ToString()) End Function 'CanConvertFrom Public Overrides Function ConvertTo( _ ByVal ctx As ITypeDescriptorContext, _ ByVal ci As CultureInfo, ByVal value As Object, _ ByVal type As Type) As Object ValidateType(value, GetType(TimeSpan)) Dim data As Long = _ Fix(CType(value, TimeSpan).TotalMinutes) Return data.ToString(CultureInfo.InvariantCulture) End Function 'ConvertTo Public Overrides Function ConvertFrom( _ ByVal ctx As ITypeDescriptorContext, _ ByVal ci As CultureInfo, ByVal data As Object) As Object Dim min As Long = _ Long.Parse(CStr(data), CultureInfo.InvariantCulture) Return TimeSpan.FromMinutes(System.Convert.ToDouble(min)) End Function 'ConvertFrom End Class 'CustomTimeSpanMinutesConverter
The following code example shows how to define a custom section that uses the previous custom TimeSpan converter.
' Define a custom section. NotInheritable Public Class CustomSection Inherits ConfigurationSection Public Sub New() End Sub 'NewNew <ConfigurationProperty("fileName", _ DefaultValue:=" default.txt "), _ TypeConverter(GetType(WhiteSpaceTrimStringConverter))> _ Public Property FileName() As String Get Return CStr(Me("fileName")) End Get Set(ByVal value As String) Me("fileName") = value End Set End Property <ConfigurationProperty("maxIdleTime"), _ TypeConverter(GetType(CustomizedTimeSpanMinutesConverter))> _ Public Property MaxIdleTime() As TimeSpan Get Return CType(Me("maxIdleTime"), TimeSpan) End Get Set(ByVal value As TimeSpan) Me("maxIdleTime") = value End Set End Property <ConfigurationProperty("timeDelay", _ DefaultValue:="infinite"), _ TypeConverter(GetType(InfiniteTimeSpanConverter))> _ Public Property TimeDelay() As TimeSpan Get Return CType(Me("timeDelay"), TimeSpan) End Get Set(ByVal value As TimeSpan) Me("timeDelay") = Value End Set End Property <ConfigurationProperty("cdStr", _ DefaultValue:="str0, str1", _ IsRequired:=True), _ TypeConverter(GetType(CommaDelimitedStringCollectionConverter))> _ Public Property CdStr() As StringCollection Get Return CType(Me("cdStr"), StringCollection) End Get Set(ByVal value As StringCollection) Me("cdStr") = value End Set End Property Public Enum Permissions FullControl = 0 Modify = 1 ReadExecute = 2 Read = 3 Write = 4 SpecialPermissions = 5 End Enum 'Permissions <ConfigurationProperty("permission", _ DefaultValue:=Permissions.Read)> _ Public Property Permission() As Permissions Get Return CType(Me("permission"), Permissions) End Get Set(ByVal value As Permissions) Me("permission") = Value End Set End Property <ConfigurationProperty("maxUsers", _ DefaultValue:="infinite"), _ TypeConverter(GetType(InfiniteIntConverter))> _ Public Property MaxUsers() As Integer Get Return Fix(Me("maxUsers")) End Get Set(ByVal value As Integer) Me("maxUsers") = Value End Set End Property End Class 'CustomSection
The following code example shows how to create and modify a configuration file using the previous custom section.
Imports System Imports System.Collections.Generic Imports System.Text Imports System.Configuration Imports System.Globalization Imports System.ComponentModel ' Define a custom section. NotInheritable Public Class CustomSection Inherits ConfigurationSection Public Sub New() End Sub 'NewNew <ConfigurationProperty("fileName", _ DefaultValue:="default.txt", _ IsRequired:=True, IsKey:=False), _ StringValidator(InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _ MinLength:=1, MaxLength:=60)> _ Public Property FileName() As String Get Return CStr(Me("fileName")) End Get Set(ByVal value As String) Me("fileName") = value End Set End Property <ConfigurationProperty("maxIdleTime"), _ TypeConverter(GetType(TsMinutesConverter))> _ Public Property MaxIdleTime() As TimeSpan Get Return CType(Me("maxIdleTime"), TimeSpan) End Get Set(ByVal value As TimeSpan) Me("maxIdleTime") = Value End Set End Property End Class 'CustomSection NotInheritable Public Class TsMinutesConverter Inherits ConfigurationConverterBase Friend Function ValidateType(value As Object, expected As Type) As Boolean Dim result As Boolean If Not (value Is Nothing) _ AndAlso (value.GetType().Equals(expected) <> True) Then result = False Else result = True End If Return result End Function 'ValidateType Public Overrides Function CanConvertTo( _ ByVal ctx As ITypeDescriptorContext, _ ByVal type As Type) As Boolean Return type.Equals(GetType(String)) End Function 'CanConvertTo Public Overrides Function CanConvertFrom( _ ByVal ctx As ITypeDescriptorContext, _ ByVal type As Type) As Boolean Return type.Equals(GetType(String)) End Function 'CanConvertFrom Public Overrides Function ConvertTo( _ ByVal ctx As ITypeDescriptorContext, ByVal ci As CultureInfo, _ ByVal value As Object, ByVal type As Type) As Object ValidateType(value, GetType(TimeSpan)) Dim data As Long = _ Fix(CType(value, TimeSpan).TotalMinutes) Return data.ToString(CultureInfo.InvariantCulture) End Function 'ConvertTo Public Overrides Function ConvertFrom( _ ByVal ctx As ITypeDescriptorContext, ByVal ci As CultureInfo, _ ByVal data As Object) As Object Dim min As Long = _ Long.Parse(CStr(data), _ CultureInfo.InvariantCulture) Return TimeSpan.FromMinutes( _ System.Convert.ToDouble(min)) End Function 'ConvertFrom End Class 'TsMinutesConverter Class UsingConfigutationConverterBase ' Create a custom section. Shared Sub CreateSection() Try Dim customSection As CustomSection ' Get the current configuration file. Dim config _ As System.Configuration.Configuration = _ ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) ' Create the section entry ' in the <configSections> and the ' related target section in <configuration>. If config.Sections("CustomSection") Is Nothing Then customSection = New CustomSection() config.Sections.Add("CustomSection", customSection) customSection.SectionInformation.ForceSave = True config.Save(ConfigurationSaveMode.Full) End If Catch err As ConfigurationErrorsException Console.WriteLine(err.ToString()) End Try End Sub 'CreateSection ' Change custom section and write it to disk. Shared Sub SerializeCustomSection() Try Dim config _ As System.Configuration.Configuration = _ ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) Dim customSection _ As CustomSection = _ config.Sections.Get("CustomSection") Dim ts As New TimeSpan(1, 30, 30) customSection.MaxIdleTime = ts config.Save() Dim maxIdleTime As String = _ customSection.MaxIdleTime.ToString() Console.WriteLine( _ "New max idle time: {0}", maxIdleTime) Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'SerializeCustomSection ' Read custom section from disk. Shared Sub DeserializeCustomSection() Try Dim config _ As System.Configuration.Configuration = _ ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) Dim customSection _ As CustomSection = _ config.Sections.Get("CustomSection") Dim maxIdleTime As TimeSpan = _ customSection.MaxIdleTime Console.WriteLine( _ "Max idle time: {0}", maxIdleTime.ToString()) Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'DeserializeCustomSection Public Overloads Shared Sub Main(ByVal args() As String) CreateSection() SerializeCustomSection() DeserializeCustomSection() End Sub 'Main End Class 'UsingConfigutationConverterBase
The following is a configuration excerpt as used by the previous example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection"
type="Samples.AspNet.CustomSection,
ConfigurationConverters,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="default.txt" maxIdleTime="90" />
</configuration>
Available since 2.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

