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?
#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?