From 8600e6f178f1df75904bf7983d3302a63107e864 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 4 Mar 2022 18:51:55 -0800 Subject: [PATCH 1/2] MissingFieldException F# snippet --- .../Overview/MissingMethodException.fs | 48 +++++++++++++++++++ .../MissingFieldException/Overview/fs.fsproj | 10 ++++ xml/System/MissingFieldException.xml | 2 + 3 files changed, 60 insertions(+) create mode 100644 snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs create mode 100644 snippets/fsharp/System/MissingFieldException/Overview/fs.fsproj diff --git a/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs b/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs new file mode 100644 index 00000000000..b5cd9d36e9e --- /dev/null +++ b/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs @@ -0,0 +1,48 @@ +// +open System +open System.Reflection + +type App = class end + +// +try + // Attempt to call a static DoSomething method defined in the App class. + // However, because the App class does not define this method, + // a MissingMethodException is thrown. + typeof.InvokeMember("DoSomething", BindingFlags.Static ||| BindingFlags.InvokeMethod, null, null, null) + |> ignore +with :? MissingMethodException as e -> + // Show the user that the DoSomething method cannot be called. + printfn $"Unable to call the DoSomething method: {e.Message}" +// + +// +try + // Attempt to access a static AField field defined in the App class. + // However, because the App class does not define this field, + // a MissingFieldException is thrown. + typeof.InvokeMember("AField", BindingFlags.Static ||| BindingFlags.SetField, null, null, [| box 5 |]) + |> ignore +with :? MissingFieldException as e -> + // Show the user that the AField field cannot be accessed. + printfn $"Unable to access the AField field: {e.Message}" +// + +// +try + // Attempt to access a static AnotherField field defined in the App class. + // However, because the App class does not define this field, + // a MissingFieldException is thrown. + typeof.InvokeMember("AnotherField", BindingFlags.Static ||| BindingFlags.GetField, null, null, null) + |> ignore +with :? MissingMemberException as e -> + // Notice that this code is catching MissingMemberException which is the + // base class of MissingMethodException and MissingFieldException. + // Show the user that the AnotherField field cannot be accessed. + printfn $"Unable to access the AnotherField field: {e.Message}" +// +// This code example produces the following output: +// Unable to call the DoSomething method: Method 'App.DoSomething' not found. +// Unable to access the AField field: Field 'App.AField' not found. +// Unable to access the AnotherField field: Field 'App.AnotherField' not found. +// \ No newline at end of file diff --git a/snippets/fsharp/System/MissingFieldException/Overview/fs.fsproj b/snippets/fsharp/System/MissingFieldException/Overview/fs.fsproj new file mode 100644 index 00000000000..fb8cd74bde8 --- /dev/null +++ b/snippets/fsharp/System/MissingFieldException/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/xml/System/MissingFieldException.xml b/xml/System/MissingFieldException.xml index 6e3172b3d27..a9c03b4515a 100644 --- a/xml/System/MissingFieldException.xml +++ b/xml/System/MissingFieldException.xml @@ -72,6 +72,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1"::: ]]> @@ -399,6 +400,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet3"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet3"::: ]]> From 7632752972895228c31a1ccac380cb4d5f3e4d0e Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 4 Mar 2022 18:55:33 -0800 Subject: [PATCH 2/2] Add snippet refs --- xml/System/MissingMemberException.xml | 2 ++ xml/System/MissingMethodException.xml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/xml/System/MissingMemberException.xml b/xml/System/MissingMemberException.xml index 1e7fc1b76eb..e5125caa0f7 100644 --- a/xml/System/MissingMemberException.xml +++ b/xml/System/MissingMemberException.xml @@ -76,6 +76,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1"::: ]]> @@ -557,6 +558,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet4"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet4"::: ]]> diff --git a/xml/System/MissingMethodException.xml b/xml/System/MissingMethodException.xml index 2f4fbf7a066..2feeb67152f 100644 --- a/xml/System/MissingMethodException.xml +++ b/xml/System/MissingMethodException.xml @@ -77,6 +77,7 @@ The exact timing of when statically referenced methods are loaded is unspecified :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet1"::: ]]> @@ -408,6 +409,7 @@ The exact timing of when statically referenced methods are loaded is unspecified :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/MissingMethodException/cpp/MissingMethodException.cpp" id="Snippet2"::: :::code language="csharp" source="~/snippets/csharp/System/MissingFieldException/Overview/MissingMethodException.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/MissingFieldException/Overview/MissingMethodException.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/MissingMethodException/vb/missingmethodexception.vb" id="Snippet2"::: ]]>