語言

InfiniteTimeSpanConverter 類別

定義

在字串與標準無限 TimeSpan 值之間轉換。

public ref class InfiniteTimeSpanConverter sealed : System::Configuration::ConfigurationConverterBase
public sealed class InfiniteTimeSpanConverter : System.Configuration.ConfigurationConverterBase
type InfiniteTimeSpanConverter = class
    inherit ConfigurationConverterBase
Public NotInheritable Class InfiniteTimeSpanConverter
Inherits ConfigurationConverterBase
繼承
InfiniteTimeSpanConverter

範例

以下程式碼範例說明如何定義使用該 InfiniteTimeSpanConverter 類別的自訂區段屬性。

關於實作自訂區段的完整範例程式碼,請參閱該 ConfigurationConverterBase 類別。

[ConfigurationProperty("timeDelay", 
    DefaultValue = "infinite")]
[TypeConverter(typeof(InfiniteTimeSpanConverter))]
public TimeSpan TimeDelay
{
    get
    {
        return (TimeSpan)this["timeDelay"];
    }
    set
    {
        this["timeDelay"] = value;
    }
}

<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

以下範例程式碼說明如何存取先前的自訂區段屬性。

using System;
using System.IO;
using System.ComponentModel;
using System.Configuration;

namespace Samples.AspNet
{
  
    public sealed class UsingInfiniteTimeSpanConverter
    {
        public static void GetTimeDelay()
        {
            try
            {
                CustomSection section =
                    ConfigurationManager.GetSection("CustomSection")
                    as CustomSection;
                
                Console.WriteLine("timeDelay: {0}", 
                    section.TimeDelay.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public static void SetTimeDelay()
        {
            try
            {
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
                  ConfigurationUserLevel.None);

                CustomSection section =
                    config.Sections.Get("CustomSection")
                    as CustomSection;

                TimeSpan td = 
                    new TimeSpan();

                td =
                    TimeSpan.FromMinutes(
                    DateTime.Now.Minute);

                section.TimeDelay = td;

                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full); 
                config.Save();

                Console.WriteLine("timeDelay: {0}",
                    section.TimeDelay.ToString());
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Imports System.IO
Imports System.ComponentModel
Imports System.Configuration


NotInheritable Public Class UsingInfiniteTimeSpanConverter
    
    Public Shared Sub GetTimeDelay() 
        Try
            Dim section As CustomSection = _
            ConfigurationManager.GetSection( _
            "CustomSection")
            Console.WriteLine("timeDelay: {0}", _
            section.TimeDelay.ToString())
        Catch e As System.Exception
            Console.WriteLine(e.Message)
        End Try
    
    End Sub
    
    
    Public Shared Sub SetTimeDelay() 
        Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)
            
            Dim section As CustomSection = _
            config.Sections.Get("CustomSection")
            
            Dim td As New TimeSpan()
            
            td = _
            TimeSpan.FromMinutes( _
            DateTime.Now.Minute)
            
            section.TimeDelay = td
            
            section.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
            config.Save()
            
            Console.WriteLine("timeDelay: {0}", _
            section.TimeDelay.ToString())
        Catch e As System.Exception
            Console.WriteLine(e.Message)
        End Try
    
    End Sub
End Class

以下是前述範例所使用的配置摘錄。

<?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" timeDelay="infinite"/>
</configuration>

備註

此類型與其他組態轉換器類型一樣,會將組態檔案中的字串轉換為與相關強型別屬性的連結。

特別是,InfiniteTimeSpanConverter將String分配給配置屬性的「無限」值轉換為標準無限TimeSpan,反之亦然。 無限 TimeSpan 值由列舉值表示 MaxValue 。

此轉換器用於性質 TimeSpan 。 無限值會被持續保存為「無限」字串。

建構函式

名稱 Description
InfiniteTimeSpanConverter()

初始化 InfiniteTimeSpanConverter 類別的新執行個體。

方法

名稱 Description
CanConvertFrom(ITypeDescriptorContext, Type)

決定是否允許轉換。

(繼承來源 ConfigurationConverterBase)
CanConvertFrom(Type)

回傳此轉換器是否能將指定類型的物件轉換為該轉換器的類型。

(繼承來源 TypeConverter)
CanConvertTo(ITypeDescriptorContext, Type)

決定是否允許轉換。

(繼承來源 ConfigurationConverterBase)
CanConvertTo(Type)

回傳此轉換器是否能將物件轉換為指定類型。

(繼承來源 TypeConverter)
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

將 String 轉換成 TimeSpan。

ConvertFrom(Object)

將給定值轉換為該轉換器的類型。

(繼承來源 TypeConverter)
ConvertFromInvariantString(ITypeDescriptorContext, String)

利用不變文化與指定的上下文,將給定字串轉換為該轉換器的類型。

(繼承來源 TypeConverter)
ConvertFromInvariantString(String)

利用不變培養法將給定字串轉換為該轉換器的類型。

(繼承來源 TypeConverter)
ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

利用指定的上下文與文化資訊,將給定文本轉換為物件。

(繼承來源 TypeConverter)
ConvertFromString(ITypeDescriptorContext, String)

將給定文字轉換為物件,並依照指定的上下文。

(繼承來源 TypeConverter)
ConvertFromString(String)

將指定的文字轉換成物件。

(繼承來源 TypeConverter)
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

將 TimeSpan 轉換成 String。

ConvertTo(Object, Type)

利用參數將給定值物件轉換為指定的型別。

(繼承來源 TypeConverter)
ConvertToInvariantString(ITypeDescriptorContext, Object)

將指定的值轉換為文化不變的字串表示法,並依照指定的上下文進行。

(繼承來源 TypeConverter)
ConvertToInvariantString(Object)

將指定的值轉換為文化不變的字串表示。

(繼承來源 TypeConverter)
ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

利用指定的上下文與文化資訊,將給定值轉換為字串表示。

(繼承來源 TypeConverter)
ConvertToString(ITypeDescriptorContext, Object)

利用給定上下文將給定值轉換為字串表示。

(繼承來源 TypeConverter)
ConvertToString(Object)

將指定的值轉換為字串表示。

(繼承來源 TypeConverter)
CreateInstance(IDictionary)

為物件重建 Object 一組給定的屬性值。

(繼承來源 TypeConverter)
CreateInstance(ITypeDescriptorContext, IDictionary)

根據指定的上下文,根據物件的屬性值,建立該物件所關聯類型的 TypeConverter 實例。

(繼承來源 TypeConverter)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetConvertFromException(Object)

當無法執行轉換時,回傳一個例外以拋出。

(繼承來源 TypeConverter)
GetConvertToException(Object, Type)

當無法執行轉換時,回傳一個例外以拋出。

(繼承來源 TypeConverter)
GetCreateInstanceSupported()

回傳變更此物件值是否需要呼叫 CreateInstance(IDictionary) 該方法以建立新值。

(繼承來源 TypeConverter)
GetCreateInstanceSupported(ITypeDescriptorContext)

回傳是否需要在指定上下文下, CreateInstance(IDictionary) 變更此物件的值以建立新值。

(繼承來源 TypeConverter)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetProperties(ITypeDescriptorContext, Object, Attribute[])

回傳一組由值參數指定的陣列類型屬性,使用指定的上下文與屬性。

(繼承來源 TypeConverter)
GetProperties(ITypeDescriptorContext, Object)

回傳一組由值參數指定的陣列類型屬性,並依照指定的上下文。

(繼承來源 TypeConverter)
GetProperties(Object)

回傳一組由值參數指定的陣列類型屬性。

(繼承來源 TypeConverter)
GetPropertiesSupported()

回傳此物件是否支援屬性。

(繼承來源 TypeConverter)
GetPropertiesSupported(ITypeDescriptorContext)

回傳該物件是否支援屬性,並依照指定的上下文。

(繼承來源 TypeConverter)
GetStandardValues()

從預設上下文回傳一組標準值,針對此類型轉換器所設計的資料型別。

(繼承來源 TypeConverter)
GetStandardValues(ITypeDescriptorContext)

當提供格式上下文時,回傳一組標準值,針對此類型轉換器所設計的資料型別。

(繼承來源 TypeConverter)
GetStandardValuesExclusive()

回傳從 GetStandardValues() 返回的標準值集合是否為排他列表。

(繼承來源 TypeConverter)
GetStandardValuesExclusive(ITypeDescriptorContext)

回傳從 返回的 GetStandardValues() 標準值集合是否為一個可能值的排他清單,且需使用指定的上下文。

(繼承來源 TypeConverter)
GetStandardValuesSupported()

回傳此物件是否支援可從列表中選取的標準值集合。

(繼承來源 TypeConverter)
GetStandardValuesSupported(ITypeDescriptorContext)

回傳此物件是否支援可從指定上下文中選擇的標準值集合。

(繼承來源 TypeConverter)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
IsValid(ITypeDescriptorContext, Object)

回傳給定值物件是否適用於此類型及指定上下文。

(繼承來源 TypeConverter)
IsValid(Object)

回傳給定值物件是否適用於此類型。

(繼承來源 TypeConverter)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
SortProperties(PropertyDescriptorCollection, String[])

整理一組屬性。

(繼承來源 TypeConverter)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱