InfiniteTimeSpanConverter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在字串以及標準無限 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 自定義區段屬性。
如需實作自定義區段的完整範例程式代碼,請參閱 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 使用。 無限值會保存為 「infinite」 字串。
建構函式
InfiniteTimeSpanConverter() |
初始化 InfiniteTimeSpanConverter 類別的新執行個體。 |