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.

Call static contructors when assembly is loading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hyd - 09 Mar 2006 09:48 GMT
With C++/CLI - VS2005, is it possible to have static constructors that
are automatically called when the owner assembly is loading ?
Otherwise, how it is possible to call it without creating an instance
of the class or calling a static field of this class ?

I need to "register" some classes in a list of classes so that I can
create them without knowing them.
Bruno van Dooren - 09 Mar 2006 17:33 GMT
> With C++/CLI - VS2005, is it possible to have static constructors that
> are automatically called when the owner assembly is loading ?
> Otherwise, how it is possible to call it without creating an instance
> of the class or calling a static field of this class ?

yes. static constructors are possible. they are called the when the class is
referenced for the first time.
Check out the thread 'Static constructors/destructors' from 25 jan in this
newsgroup.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

hyd - 09 Mar 2006 19:39 GMT
Thank you, but I wasn't be very clear.
What I want, it's that static contructors are called without
referencing the class.

I have a sample :
---------------------------------------------------------
using namespace System;
using namespace System::Collections::Generic;

ref class BaseClass abstract
{
};

ref class IClassFactory abstract
{
 static Dictionary<long, IClassFactory^> sClassFactories;
public:
 IClassFactory( long iClassCode )
 {
   sClassFactories.Add( iClassCode, this );
 }
 virtual BaseClass^ Create() = 0;
 static BaseClass^ CreateClass( long iClassCode )
 {
   if( sClassFactories.ContainsKey( iClassCode ) )
     return sClassFactories[iClassCode]->Create();
   return nullptr;
 }
};

generic<class TClass> where TClass:BaseClass
ref class ClassFactory : public IClassFactory
{
public:
 ClassFactory( long iClassCode ) : IClassFactory( iClassCode ){}
 virtual BaseClass^ Create() override
 {
   return System::Activator::CreateInstance<TClass>();
 }
};

ref class ClassA : BaseClass
{
 static IClassFactory^ sClassFactory = gcnew ClassFactory<ClassA^>(
458 );
public:
 int A;
};

ref class ClassB : BaseClass
{
 static IClassFactory^ sClassFactory = gcnew ClassFactory<ClassB^>(
854 );
public:
 double B;
};

int main(array<System::String ^> ^args)
{
 //ClassA^ mpclassA = gcnew ClassA();
 BaseClass^ baseClass = IClassFactory::CreateClass( 458 ); // return
nullptr pointer, but it returns an instance of ClassA if I uncomment
the previous line.
 return 0;
}
---------------------------------------------------------

I want that IClassFactory::CreateClass( 458 ); return an instance of
ClassA without the commented line.
Holger Grund - 09 Mar 2006 20:08 GMT
> With C++/CLI - VS2005, is it possible to have static constructors that
> are automatically called when the owner assembly is loading ?
> Otherwise, how it is possible to call it without creating an instance
> of the class or calling a static field of this class ?

You can define the module constructor, which is called when the
assembly is loaded (you define it with __identifier(".cctor").
You'll need to disable the discretionary error)

However, VC++ use that one for initialization itself (unless you build
with /clr:safe). You'll lose that functionality.

-hg
Marcus Heege - 11 Mar 2006 16:12 GMT
>> With C++/CLI - VS2005, is it possible to have static constructors that
>> are automatically called when the owner assembly is loading ?
[quoted text clipped - 9 lines]
>
> -hg

You can easily get both. In a file compiled with /clr wite:

struct ModuleInit
{
 ModuleInit() { ... you initialization code goes here ... }
} g_moduleInit;

Since this defines a global variable in a managed object file, the module
.cctor will call its constructor and you get both: CTR initialization and
you initialization

Marcus Heege

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.