1
+ // <snippet1>
2
+ open System
3
+
4
+ /// Class that implements IConvertible
5
+ type Complex ( x , y ) =
6
+ member _.GetDoubleValue () =
7
+ sqrt ( x * x + y * y)
8
+
9
+ interface IConvertible with
10
+ member _.GetTypeCode () =
11
+ TypeCode.Object
12
+
13
+ member _.ToBoolean ( provider : IFormatProvider ) =
14
+ ( x <> 0. ) || ( y <> 0. )
15
+
16
+ member this.ToByte ( provider : IFormatProvider ) =
17
+ Convert.ToByte( this.GetDoubleValue())
18
+
19
+ member this.ToChar ( provider : IFormatProvider ) =
20
+ Convert.ToChar( this.GetDoubleValue())
21
+
22
+ member this.ToDateTime ( provider : IFormatProvider ) =
23
+ Convert.ToDateTime( this.GetDoubleValue())
24
+
25
+ member this.ToDecimal ( provider : IFormatProvider ) =
26
+ Convert.ToDecimal( this.GetDoubleValue())
27
+
28
+ member this.ToDouble ( provider : IFormatProvider ) =
29
+ this.GetDoubleValue()
30
+
31
+ member this.ToInt16 ( provider : IFormatProvider ) =
32
+ Convert.ToInt16( this.GetDoubleValue())
33
+
34
+ member this.ToInt32 ( provider : IFormatProvider ) =
35
+ Convert.ToInt32( this.GetDoubleValue())
36
+
37
+ member this.ToInt64 ( provider : IFormatProvider ) =
38
+ Convert.ToInt64( this.GetDoubleValue())
39
+
40
+ member this.ToSByte ( provider : IFormatProvider ) =
41
+ Convert.ToSByte( this.GetDoubleValue())
42
+
43
+ member this.ToSingle ( provider : IFormatProvider ) =
44
+ Convert.ToSingle( this.GetDoubleValue())
45
+
46
+ member _.ToString ( provider : IFormatProvider ) =
47
+ $" ({x}, {y})"
48
+
49
+ member this.ToType ( conversionType : Type , provider : IFormatProvider ) =
50
+ Convert.ChangeType( this.GetDoubleValue(), conversionType)
51
+
52
+ member this.ToUInt16 ( provider : IFormatProvider ) =
53
+ Convert.ToUInt16( this.GetDoubleValue())
54
+
55
+ member this.ToUInt32 ( provider : IFormatProvider ) =
56
+ Convert.ToUInt32( this.GetDoubleValue())
57
+
58
+ member this.ToUInt64 ( provider : IFormatProvider ) =
59
+ Convert.ToUInt64( this.GetDoubleValue())
60
+
61
+ let writeObjectInfo testObject =
62
+ let typeCode = Type.GetTypeCode( testObject.GetType())
63
+ match typeCode with
64
+ | TypeCode.Boolean ->
65
+ printfn $" Boolean: {testObject}"
66
+ | TypeCode.Double ->
67
+ printfn $" Boolean: {testObject}"
68
+ | _ ->
69
+ printfn $" {typeCode}: {testObject}"
70
+
71
+ let testComplex = Complex( 4 , 7 )
72
+
73
+ writeObjectInfo testComplex
74
+ writeObjectInfo ( Convert.ToBoolean testComplex)
75
+ writeObjectInfo ( Convert.ToDecimal testComplex)
76
+ writeObjectInfo ( Convert.ToString testComplex)
77
+ // </snippet1>
0 commit comments