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