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 分配给配置属性的值“infinite”转换为标准无限 TimeSpan 值,反之亦然。 无限 TimeSpan 值由 MaxValue 枚举值表示。
此转换器与属性一起使用 TimeSpan 。 无限值将保留为“无限”字符串。
构造函数
InfiniteTimeSpanConverter() |
初始化 InfiniteTimeSpanConverter 类的新实例。 |