From be19bc4683744a3b6a00f4b93ec42e382f0cc69d Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Tue, 23 Nov 2021 20:37:45 -0800 Subject: [PATCH 1/3] System.Activator F# snippets --- .../ActivatorX/fs/ActivatorX.fs | 37 +++++++++++++ .../ActivatorX/fs/ActivatorX.fsproj | 13 +++++ .../VS_Snippets_CLR/ActivatorX/fs/source2.fs | 54 +++++++++++++++++++ .../fs/remarks.fs | 6 +++ .../fs/remarks.fsproj | 12 +++++ .../fs/CreateInstance5.fs | 18 +++++++ .../fs/PersonInfo/PersonInfo.fsproj | 12 +++++ .../fs/createinstance.fsproj | 20 +++++++ .../fs/createinstance2.fs | 24 +++++++++ .../fs/createinstanceex1.fs | 13 +++++ .../fs/createinstanceex1a.fs | 24 +++++++++ .../fs/personinfo.fs | 11 ++++ xml/System/Activator.xml | 9 ++++ 13 files changed, 253 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/source2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/PersonInfo/PersonInfo.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/personinfo.fs diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs new file mode 100644 index 00000000000..2d245486564 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs @@ -0,0 +1,37 @@ +module ActivatorX + +// +open System +open System.Reflection +open System.Text + +type SomeType() = + member _.DoSomething(x) = printfn $"100 / {x} = {100 / x}" + +// +// Create an instance of the StringBuilder type using Activator.CreateInstance. +let o = Activator.CreateInstance typeof + +// Append a string into the StringBuilder object and display the StringBuilder. +let sb = o :?> StringBuilder +sb.Append "Hello, there." |> ignore +printfn $"{sb}" +// + +// +// Create an instance of the SomeType class that is defined in this assembly. +let oh = + Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().Location, typeof.FullName) + +// Call an instance method defined by the SomeType type using this object. +let st = oh.Unwrap() :?> SomeType + +st.DoSomething 5 +// + +(* This code produces the following output: + +Hello, there. +100 / 5 = 20 + *) +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fsproj new file mode 100644 index 00000000000..2b83f846b81 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fsproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0 + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/source2.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/source2.fs new file mode 100644 index 00000000000..1b074595f45 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/source2.fs @@ -0,0 +1,54 @@ +module source2 + +// +open System + +let instanceSpec = + "System.EventArgs;System.Random;System.Exception;System.Object;System.Version" + +let instances = instanceSpec.Split ';' +let instlist = Array.zeroCreate instances.Length +let mutable item = obj () + +for i = 0 to instances.Length - 1 do + // create the object from the specification string + printfn $"Creating instance of: {instances.[i]}" + item <- Activator.CreateInstance(Type.GetType instances.[i]) + instlist.[i] <- item + +printfn "\nObjects and their default values:\n" + +for o in instlist do + printfn $"Type: {o.GetType().FullName}\nValue: {o}\nHashCode: {o.GetHashCode()}\n" + + +// This program will display output similar to the following: +// +// Creating instance of: System.EventArgs +// Creating instance of: System.Random +// Creating instance of: System.Exception +// Creating instance of: System.Object +// Creating instance of: System.Version +// +// Objects and their default values: +// +// Type: System.EventArgs +// Value: System.EventArgs +// HashCode: 46104728 +// +// Type: System.Random +// Value: System.Random +// HashCode: 12289376 +// +// Type: System.Exception +// Value: System.Exception: Exception of type 'System.Exception' was thrown. +// HashCode: 55530882 +// +// Type: System.Object +// Value: System.Object +// HashCode: 30015890 +// +// Type: System.Version +// Value: 0.0 +// HashCode: 1048575 +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fs new file mode 100644 index 00000000000..8346cfab438 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fs @@ -0,0 +1,6 @@ +module remarks + +// +let factory<'T when 'T : (new: unit -> 'T)> = + new 'T() +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fsproj new file mode 100644 index 00000000000..0f35fe6cf3f --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fsproj @@ -0,0 +1,12 @@ + + + + Library + net6.0 + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs new file mode 100644 index 00000000000..4f70304d34c --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs @@ -0,0 +1,18 @@ +module CreateInstance5 +// +open System + +// Initialize array of characters from a to z. + +let chars = Array.zeroCreate 26 + +for i = 0 to 25 do + chars[i] <- char (i + 0x0061) + +let obj = Activator.CreateInstance(typeof, chars[13..22]) + +printfn $"{obj}" + +// The example displays the following output: +// nopqrstuvw +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/PersonInfo/PersonInfo.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/PersonInfo/PersonInfo.fsproj new file mode 100644 index 00000000000..b86164b6f04 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/PersonInfo/PersonInfo.fsproj @@ -0,0 +1,12 @@ + + + + Library + net6.0 + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance.fsproj new file mode 100644 index 00000000000..06e547aa877 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance.fsproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + + + + + + + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs new file mode 100644 index 00000000000..65d558f07ce --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs @@ -0,0 +1,24 @@ +module createinstanceex2 +// +open System + +let characters = [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |] + +let arguments = + [| characters + characters[1..4] + Array.create 20 characters[1] |] + +for i = 0 to arguments.GetUpperBound 0 do + let args = arguments[i] + + let result = + Activator.CreateInstance(typeof, args) + + printfn $"{result.GetType().Name}: {result}" + +// The example displays the following output: +// String: abcdef +// String: bcde +// String: bbbbbbbbbbbbbbbbbbbb +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1.fs new file mode 100644 index 00000000000..0b2c7988d81 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1.fs @@ -0,0 +1,13 @@ +module createinstanceex1 + +// +open System + +let handle = Activator.CreateInstance("PersonInfo", "Person") +let p = handle.Unwrap() :?> Person +p.Name <- "Samuel" +printfn $"{p}" + +// The example displays the following output: +// Samuel +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs new file mode 100644 index 00000000000..4b575ae1ee2 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs @@ -0,0 +1,24 @@ +module createinstanceex1a + +// +open System + +let handle = + Activator.CreateInstance("PersonInfo", "Person") + +let p = handle.Unwrap() +let t = p.GetType() +let prop = t.GetProperty "Name" + +if not (isNull prop) then + prop.SetValue(p, "Samuel") + +let method = t.GetMethod "ToString" +let retVal = method.Invoke(p, null) + +if not (isNull retVal) then + printfn $"{retVal}" + +// The example displays the following output: +// Samuel +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/personinfo.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/personinfo.fs new file mode 100644 index 00000000000..8fabbbdf1af --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/personinfo.fs @@ -0,0 +1,11 @@ +namespace global + +// +type Person(name) = + member val Name = name with get, set + + override this.ToString() = this.Name + + new () = Person Unchecked.defaultof + +// diff --git a/xml/System/Activator.xml b/xml/System/Activator.xml index e50f789e807..a3a11f167fb 100644 --- a/xml/System/Activator.xml +++ b/xml/System/Activator.xml @@ -90,6 +90,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ActivatorX/cpp/ActivatorX.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ActivatorX/cs/ActivatorX.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ActivatorX/VB/ActivatorX.vb" id="Snippet1"::: ]]> @@ -404,6 +405,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ActivatorX/cpp/source2.cpp" id="Snippet4"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ActivatorX/cs/source2.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/source2.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ActivatorX/VB/source2.vb" id="Snippet4"::: ]]> @@ -571,16 +573,19 @@ Note: In .NET for Win The following example defines a class named `Person` in an assembly named `PersonInfo`. Note that the `Person` class has two constructors, one of which is parameterless. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activator.createinstance/cs/personinfo.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/personinfo.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activator.createinstance/vb/personinfo.vb" id="Snippet1"::: The following example calls the method to instantiate the `Person` class. It requires a reference to PersonInfo.dll to be added to the project. Because the method calls the `Person` class parameterless constructor, the example assigns a value to its `Name` property. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activator.createinstance/cs/createinstanceex1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activator.createinstance/vb/createinstanceex1.vb" id="Snippet2"::: However, is frequently called to instantiate a type that crosses machine boundaries or that is not known at design time. In this case, you cannot include a reference to the assembly in the project and cannot make early-bound calls to the type's members. To work around this limitation, the following example uses the method along with reflection to assign a value to the `Person` object's `Name` property and to display its value. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activator.createinstance/cs/createinstanceex1a.cs" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activator.createinstance/vb/createinstanceex1a.vb" id="Snippet3"::: ]]> @@ -776,11 +781,13 @@ Note: In .NET for Win The following example calls the method to create a object. It calls the constructor to instantiate a string that contains ten elements from a character array starting at the fourteenth position. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activator.createinstance/cs/CreateInstance5.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs" id="Snippet5"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activator.createinstance/vb/CreateInstance5.vb" id="Snippet5"::: The following example creates a jagged array whose elements are arguments to be passed to a constructor. The example then passes each array to the method to invoke the appropriate string constructor. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activator.createinstance/cs/createinstance2.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activator.createinstance/vb/createinstance2.vb" id="Snippet4"::: ]]> @@ -1812,6 +1819,7 @@ Note: In .NET for Win :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.activation.createinstance~~1/cpp/remarks.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/cs/remarks.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activation.createinstance~~1/fs/remarks.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.activation.createinstance~~1/vb/remarks.vb" id="Snippet1"::: In general, there is no use for the generic method in application code, because the type must be known at compile time. If the type is known at compile time, normal instantiation syntax can be used (`new` operator in C#, `New` in Visual Basic, `gcnew` in C++). If the type is not known at compile time, you can call a non-generic overload of . @@ -1899,6 +1907,7 @@ Note: In .NET for Win :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ActivatorX/cpp/ActivatorX.cpp" id="Snippet3"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ActivatorX/cs/ActivatorX.cs" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ActivatorX/fs/ActivatorX.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ActivatorX/VB/ActivatorX.vb" id="Snippet3"::: ]]> From 8b0bef588c783a295b041fa732c8d0bf20d1613d Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Tue, 23 Nov 2021 21:31:11 -0800 Subject: [PATCH 2/3] Update createinstanceex1a.fs --- .../system.activator.createinstance/fs/createinstanceex1a.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs index 4b575ae1ee2..a998afd59c4 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstanceex1a.fs @@ -1,6 +1,6 @@ module createinstanceex1a -// +// open System let handle = @@ -21,4 +21,4 @@ if not (isNull retVal) then // The example displays the following output: // Samuel -// +// From cb5c61fe01aff85c2450263af35b22f8d1ca1a7f Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Thu, 25 Nov 2021 14:04:46 -0800 Subject: [PATCH 3/3] update snippets --- .../fs/CreateInstance5.fs | 6 +----- .../fs/createinstance2.fs | 12 +++++------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs index 4f70304d34c..e0d8c6e716b 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs @@ -3,11 +3,7 @@ module CreateInstance5 open System // Initialize array of characters from a to z. - -let chars = Array.zeroCreate 26 - -for i = 0 to 25 do - chars[i] <- char (i + 0x0061) +let chars = [| 'a' .. 'z' |] let obj = Activator.CreateInstance(typeof, chars[13..22]) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs index 65d558f07ce..de61bfb3572 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs @@ -2,16 +2,14 @@ module createinstanceex2 // open System -let characters = [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |] +let chars = [| 'a' .. 'f' |] let arguments = - [| characters - characters[1..4] - Array.create 20 characters[1] |] - -for i = 0 to arguments.GetUpperBound 0 do - let args = arguments[i] + [| chars + chars[1..4] + Array.create 20 chars[1] |] +for args in arguments do let result = Activator.CreateInstance(typeof, args)