-
Notifications
You must be signed in to change notification settings - Fork 1.6k
System.AppContext F# snippets #7425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BillWagner
merged 1 commit into
dotnet:main
from
albert-du:system-appcontext-fsharp-snippets
Dec 2, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example4.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Example4 | ||
|
||
// <Snippet4> | ||
open System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyVersion("1.0.0.0")>] | ||
do () | ||
|
||
module StringLibrary = | ||
let substringStartsAt (fullString: string) (substr: string) = | ||
fullString.IndexOf(substr, StringComparison.Ordinal) | ||
|
||
// </Snippet4> | ||
|
||
// <Snippet5> | ||
let value = "The archaeologist" | ||
let substring = "archæ" | ||
|
||
let position = | ||
StringLibrary.substringStartsAt value substring | ||
|
||
if position >= 0 then | ||
printfn $"'{substring}' found in '{value}' starting at position {position}" | ||
else | ||
printfn $"'{substring}' not found in '{value}'" | ||
|
||
// The example displays the following output: | ||
// 'archæ' not found in 'The archaeologist' | ||
// </Snippet5> |
30 changes: 30 additions & 0 deletions
30
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example6.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Example6 | ||
|
||
// <Snippet6> | ||
open System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyVersion("2.0.0.0")>] | ||
do () | ||
|
||
module StringLibrary = | ||
let substringStartsAt (fullString: string) (substr: string) = | ||
fullString.IndexOf(substr, StringComparison.CurrentCulture) | ||
|
||
// </Snippet6> | ||
|
||
// <Snippet7> | ||
let value = "The archaeologist" | ||
let substring = "archæ" | ||
|
||
let position = | ||
StringLibrary.substringStartsAt value substring | ||
|
||
if position >= 0 then | ||
printfn $"'{substring}' found in '{value}' starting at position {position}" | ||
else | ||
printfn $"'{substring}' not found in '{value}'" | ||
|
||
// The example displays the following output: | ||
// 'archæ' found in 'The archaeologist' starting at position 4 | ||
// </Snippet7> |
18 changes: 18 additions & 0 deletions
18
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/Example8.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Example8 | ||
|
||
// <Snippet8> | ||
open System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyVersion("2.0.0.0")>] | ||
do () | ||
|
||
AppContext.SetSwitch("StringLibrary.DoNotUseCultureSensitiveComparison",true) | ||
|
||
module StringLibrary = | ||
let substringStartsAt (fullString: string) (substr: string) = | ||
match AppContext.TryGetSwitch "StringLibrary.DoNotUseCultureSensitiveComparison" with | ||
| true, true -> fullString.IndexOf(substr, StringComparison.Ordinal) | ||
| _ -> fullString.IndexOf(substr, StringComparison.CurrentCulture) | ||
|
||
// </Snippet8> |
20 changes: 20 additions & 0 deletions
20
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/ForConsumers1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// <Snippet10> | ||
module Example | ||
|
||
open System.IO | ||
open System.Runtime.Versioning | ||
|
||
[<assembly: TargetFramework(".NETFramework,Version=v4.6.2")>] | ||
do () | ||
|
||
Path.GetDirectoryName "file://c/temp/dirlist.txt" | ||
|> printfn "%s" | ||
|
||
|
||
// The example displays the following output: | ||
// Unhandled Exception: System.ArgumentException: The path is not of a legal form. | ||
// at System.IO.Path.NewNormalizePathLimitedChecks(String path, Int32 maxPathLength, Boolean expandShortPaths) | ||
// at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths) | ||
// at System.IO.Path.InternalGetDirectoryName(String path) | ||
// at <StartupCode$ForConsumers1>.$Example.main@() | ||
// </Snippet10> |
12 changes: 12 additions & 0 deletions
12
...harp/VS_Snippets_CLR_System/System.AppContext.Class/fs/ForConsumers1/ForConsumers1.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\ForConsumers1.fs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
29 changes: 29 additions & 0 deletions
29
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/TestValue1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module TestValue1 | ||
|
||
open System | ||
|
||
// <Snippet1> | ||
AppContext.SetSwitch("Switch.AmazingLib.ThrowOnException", true) | ||
// </Snippet1> | ||
|
||
// <Snippet2> | ||
module AmazingLib = | ||
let performAnOperation () = | ||
match AppContext.TryGetSwitch "Switch.AmazingLib.ThrowOnException" with | ||
| false, _ -> | ||
// This is the case where the switch value was not set by the application. | ||
// The library can choose to get the value of shouldThrow by other means. | ||
// If no overrides or default values are specified, the value should be 'false'. | ||
// A false value implies the latest behavior. | ||
() | ||
| true, shouldThrow -> | ||
// The library can use the value of shouldThrow to throw exceptions or not. | ||
if shouldThrow then | ||
// old code | ||
() | ||
else | ||
// new code | ||
() | ||
// </Snippet2> | ||
|
||
AmazingLib.performAnOperation () |
16 changes: 16 additions & 0 deletions
16
samples/snippets/fsharp/VS_Snippets_CLR_System/System.AppContext.Class/fs/appcontext.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="TestValue1.fs" /> | ||
<Compile Include="Example4.fs" /> | ||
<Compile Include="Example6.fs" /> | ||
<Compile Include="Example8.fs" /> | ||
<Compile Include="ForConsumers1.fs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.