Skip to content

Commit 93387de

Browse files
authored
System.Type F# snippets (#7980)
* Type F# snippets * fix snippets
1 parent bfe6aec commit 93387de

File tree

203 files changed

+4785
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

203 files changed

+4785
-2
lines changed
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="type_assembly.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <Snippet1>
2+
open System
3+
4+
let objType = typeof<Array>
5+
6+
// Print the assembly full name.
7+
printfn $"Assembly full name:\n {objType.Assembly.FullName}."
8+
9+
// Print the assembly qualified name.
10+
printfn $"Assembly qualified name:\n {objType.AssemblyQualifiedName}."
11+
// The example displays the following output if run under the .NET Framework 4.5:
12+
// Assembly full name:
13+
// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
14+
// Assembly qualified name:
15+
// System.Array, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
16+
// </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="fullname1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// <Snippet1>
2+
open System
3+
open System.Globalization
4+
5+
let showTypeInfo (t: Type) =
6+
printfn $"Name: {t.Name}"
7+
printfn $"Full Name: {t.FullName}"
8+
printfn $"ToString: {t}"
9+
printfn $"Assembly Qualified Name: {t.AssemblyQualifiedName}\n"
10+
11+
typeof<String>
12+
|> showTypeInfo
13+
14+
(typeof<ResizeArray<_>>).GetGenericTypeDefinition()
15+
|> showTypeInfo
16+
17+
let list = ResizeArray<String>()
18+
list.GetType()
19+
|> showTypeInfo
20+
21+
let v: obj = 12
22+
v.GetType()
23+
|> showTypeInfo
24+
25+
typeof<IFormatProvider>
26+
|> showTypeInfo
27+
28+
let ifmt = NumberFormatInfo.CurrentInfo
29+
ifmt.GetType()
30+
|> showTypeInfo
31+
32+
let o = Some 3
33+
o.GetType()
34+
|> showTypeInfo
35+
36+
// The example displays output like the following:
37+
// Name: String
38+
// Full Name: System.String
39+
// ToString: System.String
40+
// Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
41+
// al, PublicKeyToken=b77a5c561934e089
42+
//
43+
// Name: List`1
44+
// Full Name: System.Collections.Generic.List`1
45+
// ToString: System.Collections.Generic.List`1[T]
46+
// Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
47+
// 0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
48+
//
49+
// Name: List`1
50+
// Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
51+
// .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
52+
// ToString: System.Collections.Generic.List`1[System.String]
53+
// Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
54+
// lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
55+
// ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
56+
//
57+
// Name: Int32
58+
// Full Name: System.Int32
59+
// ToString: System.Int32
60+
// Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
61+
// l, PublicKeyToken=b77a5c561934e089
62+
//
63+
// Name: IFormatProvider
64+
// Full Name: System.IFormatProvider
65+
// ToString: System.IFormatProvider
66+
// Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
67+
// ure=neutral, PublicKeyToken=b77a5c561934e089
68+
//
69+
// Name: NumberFormatInfo
70+
// Full Name: System.Globalization.NumberFormatInfo
71+
// ToString: System.Globalization.NumberFormatInfo
72+
// Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
73+
// n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
74+
//
75+
// Name: FSharpOption`1
76+
// Full Name: Microsoft.FSharp.Core.FSharpOption`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
77+
// ToString: Microsoft.FSharp.Core.FSharpOption`1[System.Int32]
78+
// Assembly Qualified Name: Microsoft.FSharp.Core.FSharpOption`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], FSharp.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
79+
// </Snippet1>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace global
2+
3+
// <Snippet2>
4+
type A() = class end
5+
6+
type B() = inherit A()
7+
8+
type C() = inherit B()
9+
10+
module Example =
11+
[<EntryPoint>]
12+
let main _ =
13+
for t in typeof<A>.Assembly.GetTypes() do
14+
printfn $"{t.FullName} derived from: "
15+
let mutable derived = t
16+
while derived <> null do
17+
derived <- derived.BaseType
18+
if derived <> null then
19+
printfn $" {derived.FullName}"
20+
printfn ""
21+
0
22+
// The example displays the following output:
23+
// Example derived from:
24+
// System.Object
25+
//
26+
// A derived from:
27+
// System.Object
28+
//
29+
// B derived from:
30+
// A
31+
// System.Object
32+
//
33+
// C derived from:
34+
// B
35+
// A
36+
// System.Object
37+
// </Snippet2>
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+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="remarks.fs" />
9+
<Compile Include="testbasetype.fs" />
10+
<Compile Include="basetype3.fs" />
11+
</ItemGroup>
12+
</Project>
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+
type B<'U>() = class end
5+
type C<'T>() = inherit B<'T>()
6+
// </Snippet1>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module testbasetype
2+
3+
//<Snippet1>
4+
let t = typeof<int>
5+
printfn $"{t} inherits from {t.BaseType}."
6+
//</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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//<Snippet1>
2+
open System
3+
open System.Reflection
4+
open System.Collections.Generic
5+
6+
// Define a base class with two type parameters.
7+
type Base<'T, 'U>() = class end
8+
9+
// Define a derived class. The derived class inherits from a constructed
10+
// class that meets the following criteria:
11+
// (1) Its generic type definition is Base<T, U>.
12+
// (2) It specifies int for the first type parameter.
13+
// (3) For the second type parameter, it uses the same type that is used
14+
// for the type parameter of the derived class.
15+
// Thus, the derived class is a generic type with one type parameter, but
16+
// its base class is an open constructed type with one type argument and
17+
// one type parameter.
18+
type Derived<'V>() = inherit Base<int, 'V>()
19+
20+
let displayGenericTypeInfo (t: Type) =
21+
printfn $"\n{t}"
22+
printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
23+
printfn $"\tIs it a generic type? {t.IsGenericType}"
24+
printfn $"\tDoes it have unassigned generic parameters? {t.ContainsGenericParameters}"
25+
26+
if t.IsGenericType then
27+
// If this is a generic type, display the type arguments.
28+
let typeArguments = t.GetGenericArguments()
29+
30+
printfn $"\tList type arguments ({typeArguments.Length}):"
31+
32+
for tParam in typeArguments do
33+
// IsGenericParameter is true only for generic type
34+
// parameters.
35+
if tParam.IsGenericParameter then
36+
printfn $"\t\t{tParam} (unassigned - parameter position {tParam.GenericParameterPosition})"
37+
else
38+
printfn $"\t\t{tParam}"
39+
40+
printfn $"\r\n--- Display a generic type and the open constructed"
41+
printfn $" type from which it is derived."
42+
43+
// Create a Type object representing the generic type definition
44+
// for the Derived type, by omitting the type argument. (For
45+
// types with multiple type parameters, supply the commas but
46+
// omit the type arguments.)
47+
//
48+
let derivedType = (typeof<Derived<_>>).GetGenericTypeDefinition()
49+
displayGenericTypeInfo derivedType
50+
51+
// Display its open constructed base type.
52+
displayGenericTypeInfo derivedType.BaseType
53+
54+
(* This example produces the following output:
55+
56+
--- Display a generic type and the open constructed
57+
type from which it is derived.
58+
59+
Derived`1[V]
60+
Is this a generic type definition? True
61+
Is it a generic type? True
62+
Does it have unassigned generic parameters? True
63+
List type arguments (1):
64+
V (unassigned - parameter position 0)
65+
66+
Base`2[System.Int32,V]
67+
Is this a generic type definition? False
68+
Is it a generic type? True
69+
Does it have unassigned generic parameters? True
70+
List type arguments (2):
71+
System.Int32
72+
V (unassigned - parameter position 0)
73+
*)
74+
//</Snippet1>

0 commit comments

Comments
 (0)