Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

Reflection on const variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stabiplan BV - 22 Mar 2006 15:09 GMT
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;
}
Ben Voigt - 23 Mar 2006 16:07 GMT
> 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,
>
[quoted text clipped - 8 lines]
>
> const int test = 10023;

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

> int main(array<System::String ^> ^args)
> {
[quoted text clipped - 16 lines]
>    return 0;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.