TimeSpanMinutesConverter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
轉換以分鐘表示的時間範圍。
public ref class TimeSpanMinutesConverter : System::Configuration::ConfigurationConverterBase
public class TimeSpanMinutesConverter : System.Configuration.ConfigurationConverterBase
type TimeSpanMinutesConverter = class
inherit ConfigurationConverterBase
Public Class TimeSpanMinutesConverter
Inherits ConfigurationConverterBase
- 繼承
- 衍生
範例
下列程式代碼範例示範如何定義自定義 TimeSpanMinutesConverter 類型。
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);
}
}
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
以下是上一個範例所使用的組態摘錄。
<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>
備註
此類型就像所有其他組態轉換子類型一樣,會將組態檔中找到的字串轉換成相關強型別屬性,
特別是,會將TimeSpanMinutesConverter指派給組態屬性的分鐘數轉換為StringTimeSpan分鐘數,反之亦然。
類型 TimeSpanMinutesConverter 的值 long
會保存代表分鐘數的值。
建構函式
TimeSpanMinutesConverter() |
初始化 TimeSpanMinutesConverter 類別的新執行個體。 |