Dzm*_*oda 5 .net c# f# native unmanaged
为什么我不能在F#中使用通用的非托管结构?可能Cell<'T when 'T: unmanaged>是不是没有管理,那么我怎么能解决这个问题?
type FloatCell =
struct
val x: float
val y: nativeptr<FloatCell>
end
[<Struct>]
[<StructLayout(LayoutKind.Sequential)>]
type Cell<'T when 'T: unmanaged> =
struct
val x: 'T
val y: nativeptr<Cell<'T>>
end
Run Code Online (Sandbox Code Playgroud)
给
错误FS0001:通用构造要求类型'Cell <'T>'是非托管类型[E:\ dzmitry\src\uncorefx\src\uncorefx\uncorefx.fsproj]
更新:
C#也一样.
unsafe struct FloatCell
{
public float val;
public FloatCell* next;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
unsafe struct Cell<T> where T: unmanaged
{
public float val;
public Cell<T>* next;
}
Run Code Online (Sandbox Code Playgroud)
有错误:
错误CS0208:无法获取地址,获取大小或声明指向托管类型的指针('Program.Cell')
我不认为它是管理的.
UPDATE2:
我试过属性.没有帮助.我使用了扩展属性进行强制转换.可能解决方案 但质疑为什么我不能原生那样做?或者我能做到吗?或者我应该提出C#/ F#问题?
[<Struct>]
[<NativeCppClass>]
[<System.Runtime.CompilerServices.UnsafeValueType>]
[<StructLayout(LayoutKind.Sequential)>]
type Cell<'T when 'T: unmanaged> =
struct
val element: 'T
val next: voidptr
end
type Cell<'T when 'T: unmanaged> with
member x.Next = x.next |> NativePtr.ofVoidPtr<'T>
Run Code Online (Sandbox Code Playgroud)
UPDATE3:
我试图结束指针,并没有指针进入问题.
public struct UnmanagedStruct
{
}
public struct UnmanagedStructWithSpecifiedGenerics
{
public EmptyCell<float> cell;
}
public ref struct RefUnmanagedStruct
{
public EmptyCell<float> cell;
}
public struct EmptyCell<T> where T : unmanaged
{
}
Run Code Online (Sandbox Code Playgroud)
然后实例化:
var compiles1 = new UnmanagedStructWithSpecifiedGenerics();
var compiles2 = new EmptyCell<UnmanagedStruct>();
var CS8377_1 = new EmptyCell<EmptyCell<float>>();
var CS8377_1 = new EmptyCell<UnmanagedStructWithSpecifiedGenerics>();
var CS0306 = new EmptyCell<RefUnmanagedStruct>();
Run Code Online (Sandbox Code Playgroud)
导致:
错误CS8377:类型'EmptyCell'必须是不可为空的值类型,以及任何嵌套级别的所有字段,以便在泛型类型或方法'EmptyCell'中将其用作参数'T'
错误CS8377:类型'UnmanagedStructWithSpecifiedGenerics'必须是不可为空的值类型,以及任何嵌套级别的所有字段,以便在泛型类型或方法'EmptyCell'中将其用作参数'T'
错误CS0306:类型"RefUnmanagedStruct"不能用作类型参数
错误信息错误?我应该向Roslyn编译器提出问题吗?
它似乎是设计上的,虽然我不确定限制的原因.这是F#规范的引用:
5.2.9非托管约束
一个非托管的约束有如下形式:
typar : unmanaged在约束求解期间(第14.5节),
type : unmanaged如果type是非托管的,则满足约束条件,如下所示:
- 类型
sbyte,byte,char,nativeint,unativeint,float32,float,int16,uint16,int32,uint32,int64,uint64,decimal不受管理.- 类型
nativeptr<type>是不受管理的.- 非泛型结构类型,其字段都是非托管类型,是非托管的.
注意最后一个项目中明确提到了非泛型结构类型.