Using GCHandle with Enums

G

Guest

Why can't you pin an enumeration? For example, the with the following code:

public enum MyEnum : byte
{
ValueOne = 1,
ValueTwo = 2
}

public class MyClass
{
public MyFunc()
{
MyEnum m = MyEnum.ValueOne;
GCHandle myGch = GCHandle.Alloc(m, GCHandleType.Pinned);
}
}

When you run MyFunc(), then a System.ArgumentException is thrown: "Object
contains non-primitive or non-blittable data." This seems quite odd to me;
if the underlying type of an enum is blittable (byte) then shouldn't the enum
itself be blittable too? How does one pin an enum then?

The second part of my question is, what's the best way to pin a 2D array of
enums, eg:

public MyFunc2()
{
MyEnum[,] array = new MyEnum[3, 3];
GCHandle h = GCHandle.Alloc(array[0,0], GCHandleType.Pinned); // Same
exception
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top