|
73 | 73 | The following example illustrates the use of the <xref:System.AppContext> class to allow the customer to choose the original behavior of a library method. The following is version 1.0 of a library named `StringLibrary`. It defines a `SubstringStartsAt` method that performs an ordinal comparison to determine the starting index of a substring within a larger string.
|
74 | 74 |
|
75 | 75 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/Example4.cs" id="Snippet4":::
|
| 76 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example4.fs" id="Snippet4"::: |
76 | 77 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/Example4.vb" id="Snippet4":::
|
77 | 78 |
|
78 | 79 | The following example then uses the library to find the starting index of the substring "archæ" in "The archaeologist". Because the method performs an ordinal comparison, the substring cannot be found.
|
79 | 80 |
|
80 | 81 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/Example4.cs" id="Snippet5":::
|
| 82 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example4.fs" id="Snippet5"::: |
81 | 83 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/Example4.vb" id="Snippet5":::
|
82 | 84 |
|
83 | 85 | Version 2 of the library, however, changes the `SubstringStartsAt` method to use culture-sensitive comparison.
|
84 | 86 |
|
85 | 87 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/Example6.cs" id="Snippet6":::
|
| 88 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example6.fs" id="Snippet6"::: |
86 | 89 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/Example6.vb" id="Snippet6":::
|
87 | 90 |
|
88 | 91 | When the app is recompiled to run against the new version of the library, it now reports that the substring "archæ" is found at index 4 in "The archaeologist".
|
89 | 92 |
|
90 | 93 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/Example6.cs" id="Snippet7":::
|
| 94 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example6.fs" id="Snippet7"::: |
91 | 95 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/Example6.vb" id="Snippet7":::
|
92 | 96 |
|
93 | 97 | This change can be prevented from breaking the applications that depend on the original behavior by defining an [\<AppContextSwitchOverrides>](/dotnet/framework/configure-apps/file-schema/runtime/appcontextswitchoverrides-element) switch. In this case, the switch is named `StringLibrary.DoNotUseCultureSensitiveComparison`. Its default value, `false`, indicates that the library should perform its version 2.0 culture-sensitive comparison. `true` indicates that the library should perform its version 1.0 ordinal comparison. A slight modification of the previous code allows the library consumer to set the switch to determine the kind of comparison the method performs.
|
94 | 98 |
|
95 | 99 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/Example8.cs" id="Snippet8":::
|
| 100 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example8.fs" id="Snippet8"::: |
96 | 101 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/Example8.vb" id="Snippet8":::
|
97 | 102 |
|
98 | 103 | If application can then use the following configuration file to restore the version 1.0 behavior.
|
@@ -162,6 +167,7 @@ If you set the same switch in more than one way, the order of precedence for det
|
162 | 167 | The following is a simple application that passes a file URI to the <xref:System.IO.Path.GetDirectoryName%2A?displayProperty=nameWithType> method. When run under the .NET Framework 4.6, it throws an <xref:System.ArgumentException> because `file://` is no longer a valid part of a file path.
|
163 | 168 |
|
164 | 169 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/ForConsumers1.cs" id="Snippet10":::
|
| 170 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/ForConsumers1.fs" id="Snippet10"::: |
165 | 171 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/ForConsumers1.vb" id="Snippet10":::
|
166 | 172 |
|
167 | 173 | To restore the method's previous behavior and prevent the exception, you can add the `Switch.System.IO.UseLegacyPathHandling` switch to the application configuration file for the example:
|
@@ -370,6 +376,7 @@ In .NET 5 and later versions, for bundled assemblies, the value returned is the
|
370 | 376 | The following line of code sets a switch named `Switch.AmazingLib.ThrowOnException` to `true`, which enables a legacy behavior. The library can then check whether a library consumer has set the value of the switch by calling the <xref:System.AppContext.TryGetSwitch%2A> method.
|
371 | 377 |
|
372 | 378 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/TestValue1.cs" id="Snippet1":::
|
| 379 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/TestValue1.fs" id="Snippet1"::: |
373 | 380 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/TestValue1.vb" id="Snippet1":::
|
374 | 381 |
|
375 | 382 | ]]></format>
|
@@ -494,6 +501,7 @@ In .NET 5 and later versions, for bundled assemblies, the value returned is the
|
494 | 501 | The following example determines whether a library consumer has set a switch named `Switch.AmazingLib.ThrowOnException`.
|
495 | 502 |
|
496 | 503 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/System.AppContext.Class/cs/TestValue1.cs" id="Snippet2":::
|
| 504 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/TestValue1.fs" id="Snippet2"::: |
497 | 505 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AppContext.Class/vb/TestValue1.vb" id="Snippet2":::
|
498 | 506 |
|
499 | 507 | ]]></format>
|
|
0 commit comments