Skip to content

Commit 5ace049

Browse files
authored
RuntimeTypeHandle F# snippet (#7824)
1 parent e0edde7 commit 5ace049

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
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_gettypehandle.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <Snippet1>
2+
open System
3+
4+
type MyClass1() =
5+
let x = 0
6+
member _.MyMethod() =
7+
x
8+
9+
let myClass1 = MyClass1()
10+
11+
// Get the RuntimeTypeHandle from an object.
12+
let myRTHFromObject = Type.GetTypeHandle myClass1
13+
// Get the RuntimeTypeHandle from a type.
14+
let myRTHFromType = typeof<MyClass1>.TypeHandle
15+
16+
printfn $"\nmyRTHFromObject.Value: {myRTHFromObject.Value}"
17+
printfn $"myRTHFromObject.GetType(): {myRTHFromObject.GetType()}"
18+
printfn "Get the type back from the handle..."
19+
printfn $"Type.GetTypeFromHandle(myRTHFromObject): {Type.GetTypeFromHandle myRTHFromObject}"
20+
21+
printfn $"\nmyRTHFromObject.Equals(myRTHFromType): {myRTHFromObject.Equals myRTHFromType}"
22+
23+
printfn $"\nmyRTHFromType.Value: {myRTHFromType.Value}"
24+
printfn $"myRTHFromType.GetType(): {myRTHFromType.GetType()}"
25+
printfn "Get the type back from the handle..."
26+
printfn $"Type.GetTypeFromHandle(myRTHFromType): {Type.GetTypeFromHandle myRTHFromType}"
27+
28+
// This code example produces output similar to the following:
29+
// myRTHFromObject.Value: 799464
30+
// myRTHFromObject.GetType(): System.RuntimeTypeHandle
31+
// Get the type back from the handle...
32+
// Type.GetTypeFromHandle(myRTHFromObject): MyClass1
33+
//
34+
// myRTHFromObject.Equals(myRTHFromType): True
35+
//
36+
// myRTHFromType.Value: 799464
37+
// myRTHFromType.GetType(): System.RuntimeTypeHandle
38+
// Get the type back from the handle...
39+
// Type.GetTypeFromHandle(myRTHFromType): MyClass1
40+
// </Snippet1>

xml/System/RuntimeTypeHandle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
7878
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Type_GetTypeHandle/CPP/Type_GetTypeHandle.cpp" id="Snippet1":::
7979
:::code language="csharp" source="~/snippets/csharp/System/RuntimeTypeHandle/Overview/type_gettypehandle.cs" id="Snippet1":::
80+
:::code language="fsharp" source="~/snippets/fsharp/System/RuntimeTypeHandle/Overview/type_gettypehandle.fs" id="Snippet1":::
8081
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Type_GetTypeHandle/VB/type_gettypehandle.vb" id="Snippet1":::
8182
8283
]]></format>

0 commit comments

Comments
 (0)