1 people like it.
Like the snippet!
WPF generic value converter for Discriminated Unions
A generic value converter for discriminated unions, it allows to use a single converter for any discriminated union along with facilitating the creation of new ones with a default option value for when an incompatible union case is provided.
Large part of this snippet uses http://fssnip.net/62 as a base, all credit on the ConverterBase goes to him, one of the authors also has a new version of it http://fssnip.net/7Q
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
|
//For the following snippet to work it requires a ConverterBase which is provided in this snippet http://fssnip.net/62 from a different author.
type DiscriminatedUnionTypeConverterGeneric<'a>(defaultOnError : 'a option) =
inherit ConverterBase()
let convertFunc = fun (v:obj) _ _ _ -> box (string v)
let convertBackFunc = fun (v:obj) _ _ _ ->
if ((unbox v).GetType().Name = "String") && defaultOnError.IsSome
then box defaultOnError.Value
else v
override this.Convert = convertFunc
override this.ConvertBack = convertBackFunc
type DiscriminatedUnionTypeConverter() =
inherit DiscriminatedUnionTypeConverterGeneric<ConverterBase>(None)
//Usage:
// Either use the DiscriminatedUnionTypeConverter, or create your own with a default option for when a wrong union case is provided to the converter like so:
//type ExampleDiscriminatedUnionTypeConverter() =
// inherit DiscriminatedUnionTypeConverterGeneric<ExampleDiscriminatedUnion>(Some ExampleDiscriminatedUnion.CarCase)
// In this case if a wrong union case is provided the convertBackFunc will provide the defaultOnError value.
|
Multiple items
type DiscriminatedUnionTypeConverterGeneric<'a> =
inherit obj
new : defaultOnError:'a option -> DiscriminatedUnionTypeConverterGeneric<'a>
override Convert : 'b
override ConvertBack : 'b
Full name: Script.DiscriminatedUnionTypeConverterGeneric<_>
--------------------
new : defaultOnError:'a option -> DiscriminatedUnionTypeConverterGeneric<'a>
val defaultOnError : 'a option
type 'T option = Option<'T>
Full name: Microsoft.FSharp.Core.option<_>
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
val box : value:'T -> obj
Full name: Microsoft.FSharp.Core.Operators.box
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
val unbox : value:obj -> 'T
Full name: Microsoft.FSharp.Core.Operators.unbox
override DiscriminatedUnionTypeConverterGeneric.Convert : 'b
Full name: Script.DiscriminatedUnionTypeConverterGeneric`1.Convert
override DiscriminatedUnionTypeConverterGeneric.ConvertBack : 'b
Full name: Script.DiscriminatedUnionTypeConverterGeneric`1.ConvertBack
Multiple items
type DiscriminatedUnionTypeConverter =
inherit obj
new : unit -> DiscriminatedUnionTypeConverter
Full name: Script.DiscriminatedUnionTypeConverter
--------------------
new : unit -> DiscriminatedUnionTypeConverter
union case Option.None: Option<'T>
More information