Permalink
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (81 sloc) 5.41 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
<TimeSpan_LegacyFormatMode> Element
03/30/2017
.net-framework
dotnet-clr
article
VB
CSharp
C++
jsharp
<TimeSpan_LegacyFormatMode> element
TimeSpan_LegacyFormatMode element
865e7207-d050-4442-b574-57ea29d5e2d6
8
rpetrusha
ronpet
wpickett

<TimeSpan_LegacyFormatMode> Element

Determines whether the runtime preserves legacy behavior in formatting operations with xref:System.TimeSpan?displayProperty=nameWithType values.

<configuration>
<runtime>
<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.

Specifies whether the runtime uses legacy formatting behavior with xref:System.TimeSpan?displayProperty=nameWithType 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 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

Starting with the [!INCLUDEnet_v40_long], the xref:System.TimeSpan?displayProperty=nameWithType structure implements the xref:System.IFormattable interface and supports formatting operations with standard and custom format strings. If a parsing method encounters an unsupported format specifier or format string, it throws a xref:System.FormatException.

In previous versions of the .NET Framework, the xref:System.TimeSpan structure did not implement xref:System.IFormattable and did not support format strings. However, many developers mistakenly assumed that xref:System.TimeSpan did support a set of format strings and used them in composite formatting operations with methods such as xref:System.String.Format%2A?displayProperty=nameWithType. Ordinarily, if a type implements xref:System.IFormattable and supports format strings, calls to formatting methods with unsupported format strings usually throw a xref:System.FormatException. However, because xref:System.TimeSpan did not implement xref:System.IFormattable, the runtime ignored the format string and instead called the xref:System.TimeSpan.ToString?displayProperty=nameWithType method. This means that, although the format strings had no effect on the formatting operation, their presence did not result in a xref:System.FormatException.

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 xref:System.TimeSpan behavior. When you set the enabled attribute of this element to true, the composite formatting method results in a call to xref:System.TimeSpan.ToString?displayProperty=nameWithType rather than xref:System.TimeSpan.ToString%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType, and a xref:System.FormatException is not thrown.

Example

The following example instantiates a xref:System.TimeSpan object and attempts to format it with the xref:System.String.Format%28System.String%2CSystem.Object%29?displayProperty=nameWithType method by using an unsupported standard format string.

[!code-csharpTimeSpan.BreakingChanges#1] [!code-vbTimeSpan.BreakingChanges#1]

When you run the example on the [!INCLUDEnet_v35_short] or on an earlier version, it displays the following output:

12:30:45  

This differs markedly from the output if you run the example on the [!INCLUDEnet_v40_long] or later version:

Invalid Format  

However, if you add the following configuration file to the example's directory and then run the example on the [!INCLUDEnet_v40_long] or later version, the output is identical to that produced by the example when it is run on [!INCLUDEnet_v35_short].

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

See Also

Runtime Settings Schema
Configuration File Schema