diff --git a/MvcApp/Models.fs b/MvcApp/Models.fs index 1a105d9..9a2796b 100644 --- a/MvcApp/Models.fs +++ b/MvcApp/Models.fs @@ -27,7 +27,7 @@ let parseId prefix str = | None -> raise (FormatException str) [] -[>)>] +[)>] type CustomerId = { Value : Guid } static member Default : CustomerId = { Value=Guid.Empty } @@ -42,8 +42,10 @@ type CustomerId = with _ -> result <- Unchecked.defaultof<_> false +and internal CustomerId_T1 = ParseTypeConverter + [] -[>)>] +[)>] type ProductId = { Value : Guid } static member Default : ProductId = { Value=Guid.Empty } @@ -58,8 +60,10 @@ type ProductId = with _ -> result <- Unchecked.defaultof<_> false +and internal ProductId_T1 = ParseTypeConverter + [] -[>)>] +[)>] type OrderId = { Value : Guid } static member Default : OrderId = { Value=Guid.Empty } @@ -74,6 +78,7 @@ type OrderId = with _ -> result <- Unchecked.defaultof<_> false +and internal OrderId_T1 = ParseTypeConverter type Customer = {Id:CustomerId; FirstName:string ; LastName:string; Version:int} diff --git a/Saithe/ParseTypeConverters.fs b/Saithe/ParseTypeConverters.fs index e1fe603..f64e3c9 100644 --- a/Saithe/ParseTypeConverters.fs +++ b/Saithe/ParseTypeConverters.fs @@ -4,12 +4,14 @@ open System.ComponentModel open System open System.Reflection open Newtonsoft.Json +module internal MethodInfos= + let matchParse (t:MethodInfo) = t.Name<>null && (t.Name.Equals("Parse") || t.Name.EndsWith(".Parse")) && t.GetParameters().Length = 2 -type ParseTypeConverter<'T (* when IParsable<'T> *) >() = +type ParseTypeConverter<'T when 'T :> IParsable<'T> >() = inherit TypeConverter() let strT = typeof let t = typeof<'T> - let parse_method = t.GetInterface("IParsable`1").GetMethod("Parse") + let parse_method = t.GetMethods() |> Array.find MethodInfos.matchParse let parse s = try @@ -30,11 +32,11 @@ type ParseTypeConverter<'T (* when IParsable<'T> *) >() = if destinationType = t then box (parse value) else box (value.ToString()) -type public ParseTypeJsonConverter<'T (* when IParsable<'T> *) >() = +type public ParseTypeJsonConverter<'T when 'T :> IParsable<'T> >() = inherit JsonConverter() let t = typeof<'T> - let parse_method = t.GetInterface("IParsable`1").GetMethod("Parse") + let parse_method = t.GetMethods() |> Array.find MethodInfos.matchParse let parse s = try diff --git a/Tests/Handle_discriminated_union.fs b/Tests/Handle_discriminated_union.fs index bd06c2b..9f48b5c 100644 --- a/Tests/Handle_discriminated_union.fs +++ b/Tests/Handle_discriminated_union.fs @@ -7,9 +7,8 @@ open Newtonsoft.Json open System.ComponentModel open System.Globalization - -[>)>] -[>)>] +[)>] +[)>] type ParseValueType = | ValueType of string | Empty @@ -33,6 +32,9 @@ type ParseValueType = with _ -> result <- Unchecked.defaultof<_> false +and internal ParseValueType_T1 = ParseTypeConverter +and internal ParseValueType_T2 = ParseTypeJsonConverter + [] [] diff --git a/Tests/Parse_fs_type.fs b/Tests/Parse_fs_type.fs index 538ef5b..024be84 100644 --- a/Tests/Parse_fs_type.fs +++ b/Tests/Parse_fs_type.fs @@ -6,7 +6,7 @@ open Newtonsoft.Json open System.ComponentModel open System.Globalization -[>)>] +[)>] type ParseValueType={ Value:string } with static member Parse (str:string)= @@ -24,6 +24,7 @@ with with _ -> result <- Unchecked.defaultof<_> false +and internal ParseValueType_T1 = ParseTypeConverter [] []