有许多遗留接口以普通的形式获得实体集合IEnumerable
.通常人们可以foreach(CertainTypeWeSureItemIs item in items)
在C#中将对象转换为他们想要的任何类型.IEnumerable不会直接转换为序列.包装它seq { for x in xs -> x }
也没有多大帮助,因为它得到了seq{obj}
.那么我如何在F#中做到这一点?
Lee*_*Lee 19
用途Seq.cast<T>
:
let al = new System.Collections.ArrayList()
al.Add(1) |> ignore
al.Add(2) |> ignore
al |> Seq.cast<int> |> Seq.iter(printf "%i")
Run Code Online (Sandbox Code Playgroud)