1
1
namespace FsCodec.SystemTextJson
2
2
3
- open System
4
- open System.Linq .Expressions
5
3
open System.Text .Json .Serialization
6
4
7
- type internal ConverterActivator = delegate of unit -> JsonConverter
8
-
9
5
type UnionOrTypeSafeEnumConverterFactory ( typeSafeEnum , union ) =
10
6
inherit JsonConverterFactory()
11
7
12
- static let hasConverterAttribute = memoize ( fun ( t : Type ) -> t.IsDefined( typeof< JsonConverterAttribute>, true ))
8
+ static let hasConverterAttribute = memoize ( fun ( t : System. Type ) -> t.IsDefined( typeof< JsonConverterAttribute>, true ))
13
9
14
- override _.CanConvert ( t : Type ) =
10
+ override _.CanConvert t =
15
11
not ( t.IsGenericType && let gtd = t.GetGenericTypeDefinition() in gtd = typedefof< option<_>> || gtd = typedefof< list<_>>)
16
12
&& FsCodec.Union.isUnion t
17
13
&& not ( hasConverterAttribute t)
@@ -20,9 +16,4 @@ type UnionOrTypeSafeEnumConverterFactory(typeSafeEnum, union) =
20
16
21
17
override _.CreateConverter ( t , _options ) =
22
18
let openConverterType = if FsCodec.Union.isNullary t then typedefof< TypeSafeEnumConverter<_>> else typedefof< UnionConverter<_>>
23
- let constructor = openConverterType.MakeGenericType( t) .GetConstructors() |> Array.head
24
- let newExpression = Expression.New( constructor)
25
- let lambda = Expression.Lambda( typeof< ConverterActivator>, newExpression)
26
-
27
- let activator = lambda.Compile() :?> ConverterActivator
28
- activator.Invoke()
19
+ openConverterType.MakeGenericType( t) .GetConstructors().[ 0 ]. Invoke[||] :?> _
0 commit comments