3

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.

EDIT 2:

I've read up on MEF and this really provides a way to do what I wanted to do.

2
  • 2
    Did it ever occur to you that this is just a really bad/unworkable design?
    – x0n
    Sep 19, 2010 at 20:06
  • What's a bad design? A business tier? Should I put the business rules in the presentation layer? or did you mean the static nature? I was just thinking it would be more efficient to register one set of events for the entire data layer than a new set of events for each data context. Sep 19, 2010 at 20:24

3 Answers 3

5

Why not just make a static method, i.e. Initialize(), that wires up all the event handlers and just call MyClass.Initialize()?

3
  • 1
    Or even a HelloWorld() method. Anything will beat Reflection. Sep 19, 2010 at 20:03
  • Why are folks so opposed to reflection? this will run inside a type initializer, so the performance hit is negligible. Sep 20, 2010 at 13:20
  • I'm not opposed to reflection, but it's a last choice for most things. Having a separate initialization method is just simpler. What do you get by using reflection in this case when you can totally avoid it? There's other ways to ensure your event handlers are only hooked up once. Sep 20, 2010 at 13:46
5

You can use the RuntimeHelpers.RunClassConstructor method to run the static constructor:

RuntimeHelpers.RunClassConstructor(typeof(MyClass).TypeHandle);

However I strongly suggest using another approach to solve your problem... Richard Hein's suggestion seems perfectly good to me.

-1

Make the following correction in your code:

RuntimeHelpers.RunClassConstructor(typeof(MyClass).TypeHandle); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.