Click to See Complete Forum and Search --> : Problems with Reflection of Class with managed array


ok21
December 5th, 2005, 02:23 PM
I try to use Reflection in Managed C++ code to get contents of Class with managed array, but I get also managed array as simple variable and do not see its dimension. Here my code:

#include "stdafx.h"
#include "windows.h"
#using <mscorlib.dll>
#include "stdio.h"

using namespace System;
using namespace System::Reflection;
using namespace System::Text;
using namespace System::Collections;

public __gc class CJ
{ public :
unsigned int id;
unsigned int n_events;
};

public __gc class CMyClass
{ public :
CMyClass()
{ j = new CJ*[10]; } // it is not good decision to create managed array,
// but I do not know how to make it differently
int x;
CJ* j[];
};

template<typename T>
class PrintFieldInfo
{
public:
PrintFieldInfo()
{
FieldInfo* myFieldInfo[];
Type* myType = __typeof(T);
// Get the type and fields of FieldInfoClass.
myFieldInfo = myType->GetFields(static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance
| BindingFlags::Public));
Console::WriteLine(S"\nThe fields of FieldInfoClass are \n");
// Display the field information of FieldInfoClass.
Console::WriteLine(S"Length : {0}", __box(myFieldInfo->Length));
for (int i = 0; i < myFieldInfo->Length; i++)
{
Console::WriteLine(S"\nName : {0}", myFieldInfo[i]->Name);
Console::WriteLine(S"Declaring Type : {0}", myFieldInfo[i]->DeclaringType);
Console::WriteLine(S"IsPublic : {0}", __box(myFieldInfo[i]->IsPublic));
Console::WriteLine(S"MemberType : {0}", __box(myFieldInfo[i]->MemberType));
Console::WriteLine(S"FieldType : {0}", myFieldInfo[i]->FieldType);
String __gc* myType = myFieldInfo[i]->FieldType->FullName;

String __gc* str;
int ret;
while(1)
{
str = "CJ[]";
ret = String::Compare(myType,str);
if (!ret)
{
PrintFieldInfo<CJ>();
break;
}
break;
}
Console::WriteLine(S"IsFamily : {0}", __box(myFieldInfo[i]->IsFamily));
Console::WriteLine(S"ReflectedType : {0}", myFieldInfo[i]->ReflectedType);
}
}
};

int _tmain(int argc, _TCHAR* argv[])
{
PrintFieldInfo<CMyClass>();
}

As a result I have the following only one time instead of 10 times
(see out.txt in attachments).

Does anybody knows how to get in Reflection all array data?

darwen
December 5th, 2005, 04:11 PM
I'm sorry, but I'm horrendously confused about how your code works.

There are a few simple rules to apply to managed code.

One of them is don't use native C++ constructs unless you REALLY know what you're doing. Templates are native C++ constructs. I'm surprised this even compiles.

And in this case you don't need a templated class : you can just pass in an System::Object * and reflect on that to get the information you require. Bear in mind an array is still derived from System::Object - it's of type System::Array.

Encapsulate all managed code in managed classes : don't mix and match native classes with managed code. Or vice-versa.

Your code (which in future please put in code blocks) should be seperated so that the native is completely seperate from the managed except for method calls.

Then you might start getting the results you require.

As an aside .NET 2.0 has 'generics' which is the managed version of templates. If you insist on using such notation then I suggest you upgrade to DevStudio 2005.

Darwen.