I want to create a class with a static property as follows:
class MyClass
{
private static List<MyHandlerRegisteredHandlers =
new List<MyHandler>;
}
I then want to be able to create descendants of the MyHandler class
which will register themselves using a RegisterHandler method, which
basically adds one and only one instance of such classes to the static
list of RegisteredHandlers.
Each MyHandler will be able to deal a particular kind of object and will
have a bool HandlesObject(object aObject) method that returns true/false
to say wether it's capable of dealing with that object or not.
Finally, I want to be able to loop through an array of objects, check to
see if there is a MyHandler registered for each and, if so, call
MyHandler.DoSomething(theObject);
Initially I figured I could have each of the MyHandler descendant
classes implement a static constructor as follows:
class SomeObjectHandler : MyHandler
{
static SomeObjectHandler()
{
MyClass.RegisterHandler(new SomeObjectHandler());
}
}
However, it seems static constructors are only guaranteed to be called
sometime after the program starts and before an instance of the object
that they apply to is instantiated. In my case, the only instance of
SomeObjectHandler that will be instantiated is the one that gets
instantiated in it's own static constructor... which means that the
approach above isn't going to work.
In Delphi/ObjectPascal (which I used to use) each code unit can have
Initialization/Finalization sections that contain code which will be run
when that executable or dll is run/terminated. This is basically what I
need here. Is there any equivallent to this in .NET/c#??? Basically I
need something that allows me to write some code which is guaranteed to
be run at program startup (and which isn't in static void Main() obviously).
Thanks in advance.
Best Regards,
James Crosswell
Microforge.net LLC
http://www.microforge.net