作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个函数 foo,它采用非托管类型,然后我创建了一个通用结构,它要求类型参数是非托管的:
[<Struct>]
type Vector4<'T when 'T:unmanaged> =
val x : 'T
val y : 'T
val z : 'T
val w : 'T
new (x, y, z, w) = { x = x; y = y; z = z; w = w }
let foo<'T when 'T:unmanaged> (o:'T) =
printfn "%A" o
printfn "%d" sizeof<'T>
let bar() =
let o = Vector4<float32>(1.0f, 2.0f, 3.0f, 4.0f)
foo o // here has error
Error 4 A generic construct requires that the type 'Vector4<float32>' is an unmanaged type
The provided type must be an unmanaged type. Unmanaged types are either certain primitive types (sbyte, byte, char, nativeint, unativeint, float32, float, int16, uint16, int32, uint32, int64, uint64, or decimal), enumeration types, nativeptr<_>, or a non-generic structure whose fields are all unmanaged types.
最佳答案
The COM model does not support the concept of generic types. Consequently, generic types cannot be used directly for COM interop.
[<Struct>]
[<StructLayout(LayoutKind.Sequential)>]
type Vector4<'T when 'T:unmanaged> =
val x : 'T
val y : 'T
val z : 'T
val w : 'T
new (x, y, z, w) = { x = x; y = y; z = z; w = w }
type Vector4float = Vector4<float32>
let inline foo<'T when 'T:unmanaged> (o:'T) =
printfn "%A" o
printfn "%d" sizeof<'T>
let bar() =
let o = new Vector4float(1.0f, 2.0f, 3.0f, 4.0f)
foo o // A generic construct requires that the type 'Vector4float' is an unmanaged type
关于.net - 为什么由 blittable 类型参数化的泛型结构不是非托管类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675833/
我是一名优秀的程序员,十分优秀!