Managed Arrays of Enum 
Author Message
 Managed Arrays of Enum

Below are two alternative for allocating an managed array
of enum's. Alternative 1 uses Array.CreateInstance and 2
uses __gc new, for alt. 1 everything works fine but with
alt. 2 something is wrong with it's type information as
the de{*filter*} in VS can't display its values correctly and
Object.GetType() wont work. This presents a major problem
when for example using serialization on the array.

Should the alt. 2 work and what is going on here?

#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
using namespace System::Xml::Serialization;

__value enum E


int _tmain(void)
      // Alt. 1
      Type __gc * t = __typeof(E);
      E aE1 __gc[] = static_cast<E __gc[]>
(Array::CreateInstance(t, 3));
      // De{*filter*} show e1 for all elements in array.

      aE1[0] = E::e1;
      aE1[1] = E::e2;
      aE1[2] = E::e3;
      // De{*filter*} show e1, e2, e3 for elements in array.

      Type __gc * tE1 = aE1->GetType();
      // De{*filter*} show {"E[]"} for tE2.

      // Alt. 2
      E aE2 __gc[] = __gc new E __gc[3];
      // De{*filter*} show <undefined value> for all elements
in array.

      // Call Array::Initialize() as it's not in generated
IL code.
      // De{*filter*} show <undefined value> for all elements
in array.

      aE2[0] = E::e1;
      aE2[1] = E::e2;
      aE2[2] = E::e3;
      // De{*filter*} show <undefined value> for all elements
in array.

      Type __gc * tE2 = aE2->GetType();
      // Fails with "An unhandled exception of
type 'System.ExecutionEngineException' occurred in

    return 0;


Tue, 26 Apr 2005 20:52:26 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Managed array of managed arrays of char

2. moving arrays from un-managed to managed code.

3. getting at a managed enum in an unmanaged class

4. qsorting & managing struct arrays, pointer arrays

5. best way to convert enum to array[ ]

6. enum to array

7. Enum as Array Index...

8. question about an array of enum

9. Enum and Arrays, what is the difference?

10. enum, arrays, and serialization

11. typedef enum as subset of another enum

12. enum - enum ?


Powered by phpBB® Forum Software