<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 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 Framework 中,TimeSpan 結構不會執行 IFormattable 且不支援格式字串。In previous versions of the .NET Framework, the TimeSpan structure did not implement IFormattable and did not support format strings. 不過,許多開發人員不小心認為 TimeSpan 的確支援一組格式字串,並在複合格式作業中使用 String.Format之類的方法。However, 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)的呼叫,而且不會擲回 FormatExceptionWhen 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