Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Type/Assembly/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="type_assembly.fs" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions snippets/fsharp/System/Type/Assembly/type_assembly.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <Snippet1>
open System

let objType = typeof<Array>

// Print the assembly full name.
printfn $"Assembly full name:\n {objType.Assembly.FullName}."

// Print the assembly qualified name.
printfn $"Assembly qualified name:\n {objType.AssemblyQualifiedName}."
// The example displays the following output if run under the .NET Framework 4.5:
// Assembly full name:
// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
// Assembly qualified name:
// System.Array, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Type/AssemblyQualifiedName/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="fullname1.fs" />
</ItemGroup>
</Project>
79 changes: 79 additions & 0 deletions snippets/fsharp/System/Type/AssemblyQualifiedName/fullname1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// <Snippet1>
open System
open System.Globalization

let showTypeInfo (t: Type) =
printfn $"Name: {t.Name}"
printfn $"Full Name: {t.FullName}"
printfn $"ToString: {t}"
printfn $"Assembly Qualified Name: {t.AssemblyQualifiedName}\n"

typeof<String>
|> showTypeInfo

(typeof<ResizeArray<_>>).GetGenericTypeDefinition()
|> showTypeInfo

let list = ResizeArray<String>()
list.GetType()
|> showTypeInfo

let v: obj = 12
v.GetType()
|> showTypeInfo

typeof<IFormatProvider>
|> showTypeInfo

let ifmt = NumberFormatInfo.CurrentInfo
ifmt.GetType()
|> showTypeInfo

let o = Some 3
o.GetType()
|> showTypeInfo

// The example displays output like the following:
// Name: String
// Full Name: System.String
// ToString: System.String
// Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
// al, PublicKeyToken=b77a5c561934e089
//
// Name: List`1
// Full Name: System.Collections.Generic.List`1
// ToString: System.Collections.Generic.List`1[T]
// Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
// 0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: List`1
// Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
// .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
// ToString: System.Collections.Generic.List`1[System.String]
// Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
// lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
// ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: Int32
// Full Name: System.Int32
// ToString: System.Int32
// Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
// l, PublicKeyToken=b77a5c561934e089
//
// Name: IFormatProvider
// Full Name: System.IFormatProvider
// ToString: System.IFormatProvider
// Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
// ure=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: NumberFormatInfo
// Full Name: System.Globalization.NumberFormatInfo
// ToString: System.Globalization.NumberFormatInfo
// Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
// n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: FSharpOption`1
// Full Name: Microsoft.FSharp.Core.FSharpOption`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
// ToString: Microsoft.FSharp.Core.FSharpOption`1[System.Int32]
// 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
// </Snippet1>
37 changes: 37 additions & 0 deletions snippets/fsharp/System/Type/BaseType/basetype3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace global

// <Snippet2>
type A() = class end

type B() = inherit A()

type C() = inherit B()

module Example =
[<EntryPoint>]
let main _ =
for t in typeof<A>.Assembly.GetTypes() do
printfn $"{t.FullName} derived from: "
let mutable derived = t
while derived <> null do
derived <- derived.BaseType
if derived <> null then
printfn $" {derived.FullName}"
printfn ""
0
// The example displays the following output:
// Example derived from:
// System.Object
//
// A derived from:
// System.Object
//
// B derived from:
// A
// System.Object
//
// C derived from:
// B
// A
// System.Object
// </Snippet2>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/Type/BaseType/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="remarks.fs" />
<Compile Include="testbasetype.fs" />
<Compile Include="basetype3.fs" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions snippets/fsharp/System/Type/BaseType/remarks.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module remarks

// <Snippet1>
type B<'U>() = class end
type C<'T>() = inherit B<'T>()
// </Snippet1>
6 changes: 6 additions & 0 deletions snippets/fsharp/System/Type/BaseType/testbasetype.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module testbasetype

//<Snippet1>
let t = typeof<int>
printfn $"{t} inherits from {t.BaseType}."
//</Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Type/ContainsGenericParameters/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
74 changes: 74 additions & 0 deletions snippets/fsharp/System/Type/ContainsGenericParameters/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//<Snippet1>
open System
open System.Reflection
open System.Collections.Generic

// Define a base class with two type parameters.
type Base<'T, 'U>() = class end

// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
// (1) Its generic type definition is Base<T, U>.
// (2) It specifies int for the first type parameter.
// (3) For the second type parameter, it uses the same type that is used
// for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
type Derived<'V>() = inherit Base<int, 'V>()

let displayGenericTypeInfo (t: Type) =
printfn $"\n{t}"
printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
printfn $"\tIs it a generic type? {t.IsGenericType}"
printfn $"\tDoes it have unassigned generic parameters? {t.ContainsGenericParameters}"

if t.IsGenericType then
// If this is a generic type, display the type arguments.
let typeArguments = t.GetGenericArguments()

printfn $"\tList type arguments ({typeArguments.Length}):"

for tParam in typeArguments do
// IsGenericParameter is true only for generic type
// parameters.
if tParam.IsGenericParameter then
printfn $"\t\t{tParam} (unassigned - parameter position {tParam.GenericParameterPosition})"
else
printfn $"\t\t{tParam}"

printfn $"\r\n--- Display a generic type and the open constructed"
printfn $" type from which it is derived."

// Create a Type object representing the generic type definition
// for the Derived type, by omitting the type argument. (For
// types with multiple type parameters, supply the commas but
// omit the type arguments.)
//
let derivedType = (typeof<Derived<_>>).GetGenericTypeDefinition()
displayGenericTypeInfo derivedType

// Display its open constructed base type.
displayGenericTypeInfo derivedType.BaseType

(* This example produces the following output:

--- Display a generic type and the open constructed
type from which it is derived.

Derived`1[V]
Is this a generic type definition? True
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (1):
V (unassigned - parameter position 0)

Base`2[System.Int32,V]
Is this a generic type definition? False
Is it a generic type? True
Does it have unassigned generic parameters? True
List type arguments (2):
System.Int32
V (unassigned - parameter position 0)
*)
//</Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Type/DeclaringMethod/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
103 changes: 103 additions & 0 deletions snippets/fsharp/System/Type/DeclaringMethod/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//<Snippet1>
open System.Reflection

//<Snippet2>
// Define a class with a generic method.
type Example =
static member Generic<'T>(toDisplay: 'T) =
printfn $"\r\nHere it is: {toDisplay}"
//</Snippet2>

let displayGenericMethodInfo (mi: MethodInfo) =
printfn $"\n{mi}"

//<Snippet5>
printfn $"\tIs this a generic method definition? {mi.IsGenericMethodDefinition}"
//</Snippet5>

//<Snippet6>
printfn $"\tIs it a generic method? {mi.IsGenericMethod}"
//</Snippet6>

//<Snippet7>
printfn $"\tDoes it have unassigned generic parameters? {mi.ContainsGenericParameters}"
//</Snippet7>

//<Snippet8>
// If this is a generic method, display its type arguments.
//
if mi.IsGenericMethod then
let typeArguments = mi.GetGenericArguments()

printfn $"\tList type arguments ({typeArguments.Length}):"

for tParam in typeArguments do
// IsGenericParameter is true only for generic type
// parameters.
if tParam.IsGenericParameter then
printfn $"\t\t{tParam} parameter position {tParam.GenericParameterPosition}\n\t\t declaring method: {tParam.DeclaringMethod}"
else
printfn $"\t\t{tParam}"
//</Snippet8>

printfn "\r\n--- Examine a generic method."

//<Snippet3>
// Create a Type object representing class Example, and
// get a MethodInfo representing the generic method.
//
let ex = typeof<Example>
let mi = ex.GetMethod "Generic"

displayGenericMethodInfo mi

// Assign the int type to the type parameter of the Example
// method.
//
let miConstructed = mi.MakeGenericMethod typeof<int>

displayGenericMethodInfo miConstructed
//</Snippet3>

// Invoke the method.
let args = [| box 42 |]
miConstructed.Invoke(null, args) |> ignore

// Invoke the method normally.
Example.Generic<int> 42

//<Snippet4>
// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
let miDef = miConstructed.GetGenericMethodDefinition()
printfn $"\r\nThe definition is the same: {miDef = mi}"
//</Snippet4>

(* This example produces the following output:

--- Examine a generic method.

Void Generic[T](T)
Is this a generic method definition? True
Is it a generic method? True
Does it have unassigned generic parameters? True
List type arguments (1):
T parameter position 0
declaring method: Void Generic[T](T)

Void Generic[Int32](Int32)
Is this a generic method definition? False
Is it a generic method? True
Does it have unassigned generic parameters? False
List type arguments (1):
System.Int32

Here it is: 42

Here it is: 42

The definition is the same: True

*)
//</Snippet1>
Loading