c# - How to run a method on the first call to a .NET dll? -
my scenario many classes in dll leverage common services logging. these common services need initialized. since don't know classes constructed first, include code similar in constructors
logger.configure();
question there way subscribe event when dll first loaded common services initialized there, therefore allowing classes assume common services ready use.
i looked @ assemblyloadeventhandler, example demonstrates use in context of executable, event registered in main method when application starts up.
the easy thing use class constructor (aka static constructor) in classes require it, executed once per class. still feels not right.
but encapsulate calls (as per james' suggestion), , initialization in class constructor of encapsulation class, write code once , run initialization once.
... or more complicated thing define module initializer. piece of code runs when assembly loaded, you're looking for. it's not easy access, since c# compiler doesn't provide way write 1 out of box.
this fody/moduleinit comes play. it's weaver. write class, then, after compilation, fody turns class module initializer. whether it's worth hassle particular case (over encapsulation technique) decide.
Comments
Post a Comment