Skip to content

Commit d10d4a5

Browse files
authored
System.Activator F# snippets (dotnet#7421)
* System.Activator F# snippets * Update createinstanceex1a.fs * update snippets
1 parent 80f5359 commit d10d4a5

File tree

13 files changed

+247
-0
lines changed

13 files changed

+247
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module ActivatorX
2+
3+
//<snippet1>
4+
open System
5+
open System.Reflection
6+
open System.Text
7+
8+
type SomeType() =
9+
member _.DoSomething(x) = printfn $"100 / {x} = {100 / x}"
10+
11+
//<snippet2>
12+
// Create an instance of the StringBuilder type using Activator.CreateInstance.
13+
let o = Activator.CreateInstance typeof<StringBuilder>
14+
15+
// Append a string into the StringBuilder object and display the StringBuilder.
16+
let sb = o :?> StringBuilder
17+
sb.Append "Hello, there." |> ignore
18+
printfn $"{sb}"
19+
//</snippet2>
20+
21+
//<snippet3>
22+
// Create an instance of the SomeType class that is defined in this assembly.
23+
let oh =
24+
Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().Location, typeof<SomeType>.FullName)
25+
26+
// Call an instance method defined by the SomeType type using this object.
27+
let st = oh.Unwrap() :?> SomeType
28+
29+
st.DoSomething 5
30+
//</snippet3>
31+
32+
(* This code produces the following output:
33+
34+
Hello, there.
35+
100 / 5 = 20
36+
*)
37+
//</snippet1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="ActivatorX.fs" />
10+
<Compile Include="source2.fs" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module source2
2+
3+
//<snippet4>
4+
open System
5+
6+
let instanceSpec =
7+
"System.EventArgs;System.Random;System.Exception;System.Object;System.Version"
8+
9+
let instances = instanceSpec.Split ';'
10+
let instlist = Array.zeroCreate instances.Length
11+
let mutable item = obj ()
12+
13+
for i = 0 to instances.Length - 1 do
14+
// create the object from the specification string
15+
printfn $"Creating instance of: {instances.[i]}"
16+
item <- Activator.CreateInstance(Type.GetType instances.[i])
17+
instlist.[i] <- item
18+
19+
printfn "\nObjects and their default values:\n"
20+
21+
for o in instlist do
22+
printfn $"Type: {o.GetType().FullName}\nValue: {o}\nHashCode: {o.GetHashCode()}\n"
23+
24+
25+
// This program will display output similar to the following:
26+
//
27+
// Creating instance of: System.EventArgs
28+
// Creating instance of: System.Random
29+
// Creating instance of: System.Exception
30+
// Creating instance of: System.Object
31+
// Creating instance of: System.Version
32+
//
33+
// Objects and their default values:
34+
//
35+
// Type: System.EventArgs
36+
// Value: System.EventArgs
37+
// HashCode: 46104728
38+
//
39+
// Type: System.Random
40+
// Value: System.Random
41+
// HashCode: 12289376
42+
//
43+
// Type: System.Exception
44+
// Value: System.Exception: Exception of type 'System.Exception' was thrown.
45+
// HashCode: 55530882
46+
//
47+
// Type: System.Object
48+
// Value: System.Object
49+
// HashCode: 30015890
50+
//
51+
// Type: System.Version
52+
// Value: 0.0
53+
// HashCode: 1048575
54+
//</snippet4>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module remarks
2+
3+
// <Snippet1>
4+
let factory<'T when 'T : (new: unit -> 'T)> =
5+
new 'T()
6+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="remarks.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module CreateInstance5
2+
// <Snippet5>
3+
open System
4+
5+
// Initialize array of characters from a to z.
6+
let chars = [| 'a' .. 'z' |]
7+
8+
let obj = Activator.CreateInstance(typeof<string>, chars[13..22])
9+
10+
printfn $"{obj}"
11+
12+
// The example displays the following output:
13+
// nopqrstuvw
14+
// </Snippet5>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="..\personinfo.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="createinstanceex1.fs" />
10+
<Compile Include="createinstanceex1a.fs" />
11+
<Compile Include="createinstance2.fs" />
12+
<Compile Include="CreateInstance5.fs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="PersonInfo\PersonInfo.fsproj"/>
17+
</ItemGroup>
18+
19+
20+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module createinstanceex2
2+
// <Snippet4>
3+
open System
4+
5+
let chars = [| 'a' .. 'f' |]
6+
7+
let arguments =
8+
[| chars
9+
chars[1..4]
10+
Array.create 20 chars[1] |]
11+
12+
for args in arguments do
13+
let result =
14+
Activator.CreateInstance(typeof<string>, args)
15+
16+
printfn $"{result.GetType().Name}: {result}"
17+
18+
// The example displays the following output:
19+
// String: abcdef
20+
// String: bcde
21+
// String: bbbbbbbbbbbbbbbbbbbb
22+
// </Snippet4>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module createinstanceex1
2+
3+
// <Snippet2>
4+
open System
5+
6+
let handle = Activator.CreateInstance("PersonInfo", "Person")
7+
let p = handle.Unwrap() :?> Person
8+
p.Name <- "Samuel"
9+
printfn $"{p}"
10+
11+
// The example displays the following output:
12+
// Samuel
13+
// </Snippet2>

0 commit comments

Comments
 (0)