<TimeSpan_LegacyFormatMode > 元素<TimeSpan_LegacyFormatMode> Element

确定运行时是否保留旧行为中的格式设置操作System.TimeSpan值。Determines whether the runtime preserves legacy behavior in formatting operations with System.TimeSpan values.

<configuration>\<configuration>\ <runtime>\<runtime>\ <TimeSpan_LegacyFormatMode><TimeSpan_LegacyFormatMode>

语法Syntax

<TimeSpan_LegacyFormatMode
   enabled="true|false"/>

特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

特性Attribute 描述Description
enabled 必需的特性。Required attribute.

指定运行时是否使用旧式格式设置行为与System.TimeSpan值。Specifies whether the runtime uses legacy formatting behavior with System.TimeSpan values.

enabled 特性enabled Attribute

Value 描述Description
false 在运行时不会还原旧的格式设置行为。The runtime does not restore legacy formatting behavior.
true 在运行时将还原旧的格式设置行为。The runtime restores legacy formatting behavior.

子元素Child Elements

无。None.

父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。The root element in every configuration file used by the common language runtime and .NET Framework applications.
runtime 包含有关运行时初始化选项的信息。Contains information about runtime initialization options.

备注Remarks

从.NET Framework 4,开始System.TimeSpan结构实现IFormattable接口并支持格式设置操作与标准和自定义格式字符串。Starting with the .NET Framework 4, the System.TimeSpan structure implements the IFormattable interface and supports formatting operations with standard and custom format strings. 如果分析方法遇到了不受支持的格式说明符或格式字符串,则会引发FormatExceptionIf a parsing method encounters an unsupported format specifier or format string, it throws a FormatException.

在以前版本的.NET FrameworkTimeSpan结构未实现IFormattable,并且不支持格式字符串。In previous versions of the .NET Framework, the TimeSpan structure did not implement IFormattable and did not support format strings. 但是,许多开发人员错误地假定TimeSpan并支持一组格式字符串并使用它们在复合格式设置操作等方法与String.FormatHowever, many developers mistakenly assumed that TimeSpan did support a set of format strings and used them in composite formatting operations with methods such as String.Format. 通常,如果某个类型实现IFormattable并支持格式字符串,调用格式设置方法与不受支持格式字符串通常引发FormatExceptionOrdinarily, if a type implements IFormattable and supports format strings, calls to formatting methods with unsupported format strings usually throw a FormatException. 但是,由于TimeSpan未实现IFormattable,运行时忽略格式字符串,并改为调用TimeSpan.ToString()方法。However, because TimeSpan did not implement IFormattable, the runtime ignored the format string and instead called the TimeSpan.ToString() method. 这意味着,尽管格式字符串,格式设置操作没有影响,但是它们的存在没有导致FormatExceptionThis means that, although the format strings had no effect on the formatting operation, their presence did not result in a FormatException.

对于在其中旧代码将传递的复合格式设置方法,并为无效格式字符串,而不能重新编译该代码的情况下,你可以使用<TimeSpan_LegacyFormatMode>元素来还原旧TimeSpan行为。For cases in which legacy code passes a composite formatting method and an invalid format string, and that code cannot be recompiled, you can use the <TimeSpan_LegacyFormatMode> element to restore the legacy TimeSpan behavior. 当您将设置enabled到此元素的特性true、 复合格式设置方法的调用中的结果TimeSpan.ToString()而非TimeSpan.ToString(String, IFormatProvider),和一个FormatException不会引发。When you set the enabled attribute of this element to true, the composite formatting method results in a call to TimeSpan.ToString() rather than TimeSpan.ToString(String, IFormatProvider), and a FormatException is not thrown.

示例Example

下面的示例实例化TimeSpan对象,并尝试将其与格式化String.Format(String, Object)使用不受支持的标准格式字符串的方法。The following example instantiates a TimeSpan object and attempts to format it with the String.Format(String, Object) method by using an unsupported standard format string.

using System;

public class Example
{
   public static void Main()
   {
      TimeSpan interval = new TimeSpan(12, 30, 45);
      string output;
      try {
         output = String.Format("{0:r}", interval);
      }
      catch (FormatException) {
         output = "Invalid Format";
      }
      Console.WriteLine(output);
   }
}
Module Example
   Public Sub Main()
      Dim interval As New TimeSpan(12, 30, 45)
      Dim output As String
      Try
         output = String.Format("{0:r}", interval)
      Catch e As FormatException
         output = "Invalid Format"
      End Try
      Console.WriteLine(output)
   End Sub
End Module

当.NET Framework 3.5 或更早版本上运行该示例时,它会显示以下输出:When you run the example on the .NET Framework 3.5 or on an earlier version, it displays the following output:

12:30:45

这明显不同输出如果.NET Framework 4 或更高版本上运行示例:This differs markedly from the output if you run the example on the .NET Framework 4 or later version:

Invalid Format

但是,如果将下面的配置文件添加到该示例的目录,然后在.NET Framework 4 或更高版本上运行该示例输出是与.NET Framework 3.5 上运行时生成的示例相同。However, if you add the following configuration file to the example's directory and then run the example on the .NET Framework 4 or later version, the output is identical to that produced by the example when it is run on .NET Framework 3.5.

<?xml version ="1.0"?>
<configuration>
   <runtime>
      <TimeSpan_LegacyFormatMode enabled="true"/>
   </runtime>
</configuration>

请参阅See also