开发者

Why can't I use GcHandle.Alloc to pin an array of enums

开发者 https://www.devze.com 2023-04-10 17:55 出处:网络
I want to do the following: public enum Blah { A,B } [Test,Explicit] public void TestEnumGcHandle() { var ea = new Blah[10];

I want to do the following:

public enum Blah
{
    A,B
}

[Test,Explicit]
public void TestEnumGcHandle()
{
    var ea = new Blah[10];
    GCHandle.Alloc(ea, GCHandleType.Pinned);
}

but I get:

System.开发者_如何学CArgumentException : Object contains non-primitive or non-blittable data.

Are .net Enums blittable types? (Marshalling) claims that Enums are blittable, but I can't pin an array of them. Could this be a bug in GcHandle.Alloc? I'm using .NET 3.5.


It is a little heavy-handed in my book but enums are not primitive (typeof(Blah).IsPrimitive is false) and not blittable. It is missing from the list of blittable types. The linked SO question is wrong about that. Problem is that there's no way to find out what the size of the underlying integral type for the native enum might be. Heavy handed, I think, because there certainly is a way to specify it in the managed enum type. Well, can't do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消