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!
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.)