So I have this class
public static class MyClass
{
static MyClass()
{
...
}
}
Which has no methods, fields or properties. All it does is wire up handlers to static events defined elsewhere.
Since the Type initializer never gets called, because the static class is never actually accessed, the events don't get wired up.
So I was hoping to be able to invoke the type initializer via reflection ala typeof(MyClass).TypeInitializer().Invoke(...)
which blows up, with an exception stating that MyClass
is an abstract class.
Eventually the app will have other static classes with the same format that correspond to business rules. Before anything is saved to the DB static events are fired that correspond to the type of object being saved. So if what I want to do ends up not being possible, any refactoring recommendations would have to follow that structure.
EDIT:
I may not have been quite clear on exactly what I'm trying to do. Basically I have a data layer, where you can initialize an instance of a DataContext
and then when SubmitChanges()
is called I check the ChangeSet
for for inserts/updates/deletes and trigger static events for each type that is getting inserted/updated/deleted. This all works great, I'm just looking for a way to wire up handlers to the events once when the app starts. So what I was playing with was this:
static DataContext()
{
System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace == 'Data.Business')
.ToList()
.ForEach( t => {
// invoke the static TypeInitializer here,
// so that it can wire up it's event handlers.
});
}
I could use a static Initialize
method, but since these should only ever be initialized once, I was thinking TypeInitializer
.