views:

682

answers:

1

I have a .net library dll that acts like a functional library. There are a bunch of static types along with static methods.

There is some initialization code that I need to run to set up the library ready for use.

When the assembly gets loaded is there a way to ensure that a particular method is run? Something like AppDomain.AssemblyLoad but called automatically from the assembly itself. I was thinking that maybe there is something like an AssemblyAttribute that could be used?

At the moment I have this initialization code in a static constructor but as this is a library with many entry points there is no guarantee that this particular type will be used.

Thanks!

+3  A: 

Why do you need all the data to be loaded before any of it is used, rather than just when the first type which needs it is used?

I don't believe there's any way of forcing a method to be run on assembly load, from within the assembly. You could put a static constructor in every type, but frankly I think it just makes more sense to have a single type representing that data and providing access to it - and put a static constructor on that type alone. (If you've got separate bits of data which can be used independently, perhaps create separate types for them.)

Jon Skeet
I didn't mention loading any data. I really just want to make sure that the library uses a specific DateTime converter instead of the default one. (See http://stackoverflow.com/questions/458935/extend-a-typeconverter).
sixtowns
So I just want to run this code once when the assembly is loaded: TypeDescriptor.AddAttributes(typeof(DateTime), new TypeConverterAttribute(typeof(DateTimeConverterEx)));
sixtowns
So put a static constructor in every type which uses the converter. It's unfortunate, but that's the price to be paid for static state :(
Jon Skeet
(Apologies for assuming you meant "load" by "initialize" btw.)
Jon Skeet
I thought this might be the case. The question title was misleading because it said 'data' - I've changed it. Cheers.
sixtowns