IfYouSaySo
Well-known member
So I'm trying to write an object factory, and I've got the code completed, based on how I would have written it in C++. It doesn't quite work. Here's a rundown of some classes and how they work.
The class factory object has a dictionary data member, and two member functions:
Next I have my object, and it will look something like this:
So hopefully you can see that I'm trying to take advantage of a static (shared) member variable to cause object registration at DLL load time. And of course, it doesn't work--when main calls ObjectFactory.Instance.Create("MyObjectA") an exception is thrown because the item isn't in the dictionary. My hypothesis is that a shared member variable isn't actually loaded (i.e. new isn't called against it) until the first time that an object of that type is constructed. Which would be different than c++.
So anyway, does anyone know what's going on here, and also how do I get dll load time initialization of an object? Is there some attribute, or is this not possible? I'm looking for a solution that hides the registrar class and it's instances from clients of the DLL.
The class factory object has a dictionary data member, and two member functions:
VB.NET:
class ObjectFactory
' Add name to the dictionary, where MyDelegate is the value
Sub Register(ByVal name As String, Byval x As MyDelegate)
' Do a dictionary lookup on name, and return the object constructed by the call to the delegate
Function Create(ByVal name As String) As IMyObject
End Class
Next I have my object, and it will look something like this:
VB.NET:
class MyObjectA
Implements IMyObject
Public Shared Function Create() As IMyObject
Dim x As IMyObject = New MyObjectA()
return x
End Function
Private Class Registrar
Public Sub New()
ObjectFactory.Instance.Register("MyObjectA", MyObjectA.Create)
End Sub
End Class
Private Shared theRegistrar As New Registrar()
End Class
So hopefully you can see that I'm trying to take advantage of a static (shared) member variable to cause object registration at DLL load time. And of course, it doesn't work--when main calls ObjectFactory.Instance.Create("MyObjectA") an exception is thrown because the item isn't in the dictionary. My hypothesis is that a shared member variable isn't actually loaded (i.e. new isn't called against it) until the first time that an object of that type is constructed. Which would be different than c++.
So anyway, does anyone know what's going on here, and also how do I get dll load time initialization of an object? Is there some attribute, or is this not possible? I'm looking for a solution that hides the registrar class and it's instances from clients of the DLL.