views:

67

answers:

1

I have many entry points in my assembly and I want some initialization code to be executed once per AppDomain prior to running any other code from this assembly. What would be the best way to do it?

One solution I see is to have a class with static constructor and inherit every entry point I have from it. Something like this:

public class Initializer
{
    static Initializer()
    {
        EnsureInitialized();  // Calls initialization code once and only once
    }
}

public class EntryPointOne : Initializer, IEntryPoint
{
    // Some code here
}

public class EntryPointTwo : Initializer, IEntryPoint
{
    // Some code here
}

// etc.

This lets me avoid writing boiler plate static constructors in every entry point but without multi-inheritance this is not always possible. Can you think of any other better options?

+5  A: 

Check Module initializers in C#.

Incognito
Good to know, thanks!
Konstantin Spirin