By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

Reflection on const variables

P: n/a
Hi,

I am trying to find const variables outside a class. Thur far I did not
succeed. Without const, the variable is found but with const not.

So, how can I get hold on global const variables using reflection?

Regards,

Rob

// reflection.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Reflection;

const int test = 10023;

int main(array<System::String ^> ^args)
{
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
Console::WriteLine( mod->Name );
FieldInfo^ info = mod->GetField("test",
BindingFlags::Public|
BindingFlags::NonPublic|
BindingFlags::Instance|
BindingFlags::Static|
BindingFlags::FlattenHierarchy);
if ( info ) {
Console::WriteLine( "Found: " + info->Name );
}
array<Type^>^t = mod->FindTypes( Module::FilterTypeName, "t*" );
for( int i = 0; i < t->Length; i++ ) {
Console::WriteLine( t[i]->Name );
}
Console::ReadLine();
return 0;
}
Mar 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Stabiplan BV" <de*@stabiplan.nl> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl...
Hi,

I am trying to find const variables outside a class. Thur far I did not
succeed. Without const, the variable is found but with const not.

So, how can I get hold on global const variables using reflection?
Global const variables by default have static scope, and are optimized away
by the compiler. Try explicitly adding a prototype declaring the variable
as extern.

Regards,

Rob

// reflection.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Reflection;

const int test = 10023;
Try:
extern const int test;
const int test = 10023;

int main(array<System::String ^> ^args)
{
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
Console::WriteLine( mod->Name );
FieldInfo^ info = mod->GetField("test",
BindingFlags::Public|
BindingFlags::NonPublic|
BindingFlags::Instance|
BindingFlags::Static|
BindingFlags::FlattenHierarchy);
if ( info ) {
Console::WriteLine( "Found: " + info->Name );
}
array<Type^>^t = mod->FindTypes( Module::FilterTypeName, "t*" );
for( int i = 0; i < t->Length; i++ ) {
Console::WriteLine( t[i]->Name );
}
Console::ReadLine();
return 0;
}

Mar 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.