G
greek_bill
Hi,
I quite often need to have a system of classes providing alternative
implementions of some concept and need each class to register itself
with a manger (singleton) class. A good example might be some kind of
image loader, where you wrap the functionality to load an image of a
particular format in a class and a manager class maintains an
association between file extension and handler class.
In native C++ I would generally do this by having some kind of static
variable who's contructor would register a handler with the manager.
I tried doing the same in C++/CLI and came across the problem that
static member variables (and static constructors) don't get
initialized until the first time you use that class! Which of course
is a problem for me because I never intent on using that class
directly. The manager class would give me a pointer to a base class/
interface that all handle classes derive from.
Am I doing this all wrong? Is there a better way? Is there a way to
run code at app startup (without having to have a-priori knowledge of
each class)?
On a slightly different, but relevant, note, given that managed code
has (and exposes) a lot more knowledge about type information, is
there a way to store a reference to a type? I can see how that would
be useful for class factories, where you associate, say, an integer or
a string with a type. I had a quick look around the documention and I
could see how delegates (c'tors with standard interface) or even
System::Reflection::ConstructorInfo would be relevant, but long-
winded. Ideally something like :
Dictionary<int, Type>^ factory;
Type^ obj = factory[5]();
Thanks,
Bill
I quite often need to have a system of classes providing alternative
implementions of some concept and need each class to register itself
with a manger (singleton) class. A good example might be some kind of
image loader, where you wrap the functionality to load an image of a
particular format in a class and a manager class maintains an
association between file extension and handler class.
In native C++ I would generally do this by having some kind of static
variable who's contructor would register a handler with the manager.
I tried doing the same in C++/CLI and came across the problem that
static member variables (and static constructors) don't get
initialized until the first time you use that class! Which of course
is a problem for me because I never intent on using that class
directly. The manager class would give me a pointer to a base class/
interface that all handle classes derive from.
Am I doing this all wrong? Is there a better way? Is there a way to
run code at app startup (without having to have a-priori knowledge of
each class)?
On a slightly different, but relevant, note, given that managed code
has (and exposes) a lot more knowledge about type information, is
there a way to store a reference to a type? I can see how that would
be useful for class factories, where you associate, say, an integer or
a string with a type. I had a quick look around the documention and I
could see how delegates (c'tors with standard interface) or even
System::Reflection::ConstructorInfo would be relevant, but long-
winded. Ideally something like :
Dictionary<int, Type>^ factory;
Type^ obj = factory[5]();
Thanks,
Bill