fork(2) 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. if col=[] then []
  11. else
  12. let names = "Is" + str
  13. col |> List.filter(fun x-> let t = x.GetType()
  14. if t.GetProperty(names) = null then false
  15. else
  16. t.InvokeMember(names, BindingFlags.GetProperty, null, x, null) :?> bool)
  17. |> List.map(fun y ->
  18. y.GetType().InvokeMember("get_Item", BindingFlags.InvokeMethod, null, y, null))
  19.  
  20. sp collection "A" |> printfn "%A\n"
  21. sp collection "B" |> printfn "%A\n"
  22. sp collection "C" |> printfn "%A\n"
Success #stdin #stdout 0.18s 26480KB
stdin
Standard input is empty
stdout
[10; 40]

["abc"; "120"]

[]