Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6af986

Browse files
authored
System.MissingFieldException F# snippet (#7793)
* MissingFieldException F# snippet * Add snippet refs
1 parent 83cf6d2 commit f6af986

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//<snippet1>
2+
open System
3+
open System.Reflection
4+
5+
type App = class end
6+
7+
//<snippet2>
8+
try
9+
// Attempt to call a static DoSomething method defined in the App class.
10+
// However, because the App class does not define this method,
11+
// a MissingMethodException is thrown.
12+
typeof<App>.InvokeMember("DoSomething", BindingFlags.Static ||| BindingFlags.InvokeMethod, null, null, null)
13+
|> ignore
14+
with :? MissingMethodException as e ->
15+
// Show the user that the DoSomething method cannot be called.
16+
printfn $"Unable to call the DoSomething method: {e.Message}"
17+
//</snippet2>
18+
19+
//<snippet3>
20+
try
21+
// Attempt to access a static AField field defined in the App class.
22+
// However, because the App class does not define this field,
23+
// a MissingFieldException is thrown.
24+
typeof<App>.InvokeMember("AField", BindingFlags.Static ||| BindingFlags.SetField, null, null, [| box 5 |])
25+
|> ignore
26+
with :? MissingFieldException as e ->
27+
// Show the user that the AField field cannot be accessed.
28+
printfn $"Unable to access the AField field: {e.Message}"
29+
//</snippet3>
30+
31+
//<snippet4>
32+
try
33+
// Attempt to access a static AnotherField field defined in the App class.
34+
// However, because the App class does not define this field,
35+
// a MissingFieldException is thrown.
36+
typeof<App>.InvokeMember("AnotherField", BindingFlags.Static ||| BindingFlags.GetField, null, null, null)
37+
|> ignore
38+
with :? MissingMemberException as e ->
39+
// Notice that this code is catching MissingMemberException which is the
40+
// base class of MissingMethodException and MissingFieldException.
41+
// Show the user that the AnotherField field cannot be accessed.
42+
printfn $"Unable to access the AnotherField field: {e.Message}"
43+
//</snippet4>
44+
// This code example produces the following output:
45+
// Unable to call the DoSomething method: Method 'App.DoSomething' not found.
46+
// Unable to access the AField field: Field 'App.AField' not found.
47+
// Unable to access the AnotherField field: Field 'App.AnotherField' not found.
48+
//</snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="MissingMethodException.fs" />
9+
</ItemGroup>
10+
</Project>

xml/System/MissingFieldException.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
7373
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1":::
7474
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1":::
75+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1":::
7576
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1":::
7677
7778
]]></format>
@@ -399,6 +400,7 @@
399400
400401
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet3":::
401402
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet3":::
403+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet3":::
402404
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet3":::
403405
404406
]]></format>

xml/System/MissingMemberException.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
7777
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1":::
7878
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1":::
79+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1":::
7980
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1":::
8081
8182
]]></format>
@@ -557,6 +558,7 @@
557558
558559
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet4":::
559560
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet4":::
561+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet4":::
560562
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet4":::
561563
562564
]]></format>

xml/System/MissingMethodException.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ The exact timing of when statically referenced methods are loaded is unspecified
7777
7878
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1":::
7979
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1":::
80+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1":::
8081
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1":::
8182
8283
]]></format>
@@ -408,6 +409,7 @@ The exact timing of when statically referenced methods are loaded is unspecified
408409
409410
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet2":::
410411
:::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet2":::
412+
:::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet2":::
411413
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet2":::
412414
413415
]]></format>

0 commit comments

Comments
 (0)