在列表中的情况下,你可以使用System.Collections.IList
访问元素
open System.Collections
open System.Collections.Generic
let x = List<int>()
let y: IList = downcast (x :> obj)
这种方法也可以推广:让您的泛型类实现非泛型接口:
type IT =
abstract Value: obj
type T<'a>(a: 'a) =
member val Value = a;
interface IT with
member this.Value = upcast this.Value
如果不是一个选项(即,因为你不能在课堂上的变化),你总是可以诉诸反射
type T<'a>(a: 'a) =
member val Value = a;
type Action =
static member Do(a: T<_>) = printfn "%A" a.Value
let v = T(10)
let mi = typeof<Action>.GetMethod("Do").MakeGenericMethod(v.GetType().GetGenericArguments().[0])
mi.Invoke(null, [|v|])
你能澄清一下吗 - 你有一个已知的非对象类型的列表吗(在你的例子中是'int'),还是你有一个你知道的对象是一些未知类型的列表? – kvb 2014-10-02 18:24:19