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..e0d8c6e716b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/CreateInstance5.fs @@ -0,0 +1,14 @@ +module CreateInstance5 +// +open System + +// Initialize array of characters from a to z. +let chars = [| 'a' .. 'z' |] + +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..de61bfb3572 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.activator.createinstance/fs/createinstance2.fs @@ -0,0 +1,22 @@ +module createinstanceex2 +// +open System + +let chars = [| 'a' .. 'f' |] + +let arguments = + [| chars + chars[1..4] + Array.create 20 chars[1] |] + +for args in arguments do + 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..a998afd59c4 --- /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"::: ]]>