fork download
  1. open System.Reflection
  2.  
  3. type T =
  4. | A of int
  5. | B of string
  6.  
  7. let collection = [A 10; B "abc"; A 40; B "120"]
  8.  
  9. let sp (col: T list) (str:string) =
  10. match col with
  11. | [] -> []
  12. | _ ->
  13. let names = "Is" + str
  14. col
  15. |> List.choose (fun x ->
  16. let t = x.GetType()
  17. match t.GetProperty(names) with
  18. | null -> None
  19. | pi when unbox(pi.GetValue(x)) ->
  20. Some(t.InvokeMember("get_Item", BindingFlags.InvokeMethod, null, x, null))
  21. | _ -> None)
  22.  
  23. sp collection "A" |> printfn "%A\n"
  24. sp collection "B" |> printfn "%A\n"
  25. sp collection "C" |> printfn "%A\n"
Success #stdin #stdout 0.2s 26456KB
stdin
Standard input is empty
stdout
[10; 40]

["abc"; "120"]

[]