Call static contructors when assembly is loading

H

hyd

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.
 
B

Bruno van Dooren

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.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
H

hyd

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;
}
 
H

Holger Grund

hyd said:
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
 
M

Marcus Heege

Holger Grund said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top