Connecting Tech Pros Worldwide Forums | Help | Site Map

Using GCHandle with Enums

Hexar Anderson
Guest
 
Posts: n/a
#1: Nov 17 '05
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
}

Stelrad Doulton
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Using GCHandle with Enums


take a look at below:

http://msdn.microsoft.com/library/de...ingpinning.asp

"Hexar Anderson" <HexarAnderson@discussions.microsoft.com> wrote in message
news:B19308F0-0D2F-47FC-ADEF-BD9B4807E9E5@microsoft.com...[color=blue]
> 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
> }[/color]


Closed Thread