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