TimeSpanMinutesConverter クラス
アセンブリ: System.Configuration (system.configuration.dll 内)
構文
この型は、他のすべての構成コンバータ型と同様に、構成ファイル内に存在する文字列と、関連する厳密に型指定されたプロパティとの間の変換を行います。
特に、TimeSpanMinutesConverter は、構成プロパティに代入された String の時間 (分単位) から TimeSpan の時間 (分単位) への変換、およびその逆の変換を行います。
カスタムの TimeSpanMinutesConverter 型を定義する方法を次のコード例に示します。
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
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Globalization; using System.ComponentModel; public sealed class CustomTimeSpanMinutesConverter : ConfigurationConverterBase { internal bool ValidateType(object value, Type expected) { bool result; if ((value != null) && (value.GetType() != expected)) result = false; else result = true; return result; } public override bool CanConvertTo( ITypeDescriptorContext ctx, Type type) { return (type == typeof(string)); } public override bool CanConvertFrom( ITypeDescriptorContext ctx, Type type) { return (type == typeof(string)); } public override object ConvertTo( ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type) { ValidateType(value, typeof(TimeSpan)); long data = (long)(((TimeSpan)value).TotalMinutes); return data.ToString(CultureInfo.InvariantCulture); } public override object ConvertFrom( ITypeDescriptorContext ctx, CultureInfo ci, object data) { long min = long.Parse((string)data, CultureInfo.InvariantCulture); return TimeSpan.FromMinutes((double)min); } }
<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" timeDelay="infinite" cdStr="str0, str1" permission="Read" maxUsers="Infinite"/> </configuration>
System.ComponentModel.TypeConverter
System.Configuration.ConfigurationConverterBase
System.Configuration.TimeSpanMinutesConverter
System.Configuration.TimeSpanMinutesOrInfiniteConverter
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- TimeSpanMinutesConverter クラスのページへのリンク