0

I'm trying to make a function (any function) execute when the appropriate assembly is loaded. For example, The class Test has method the whatever that I want to be called when the Assembly containing the Test class is loaded with Assembly.Load(); (any function outside of a class if fine as well). Any ideas?

yair koskas
  • 134
  • 5
  • There's no "assembly-loaded-event" that can be catched from within that specific assembly. Why not explicitely call it from your client when you use reflection to load the assembly? Maybe that also helps: https://stackoverflow.com/questions/4020178/how-to-write-class-libs-assembly-load-init-event-handler – MakePeaceGreatAgain Nov 28 '22 at 09:21
  • I'm trying to execute custom code on `Assembly.load("...")`, can't call anything else. – yair koskas Nov 28 '22 at 09:26
  • You can load the assembly and call a specific member within a specific class after you've done so. Something like `var a = Assembly.Load(); a.GetType.GetMethod("MyMethod").Invoke(null, null);` – MakePeaceGreatAgain Nov 28 '22 at 09:28
  • Theoritically there is module initializer in CLR ... but it is not avaible from C# ... but there are some weavers which can add it – Selvin Nov 28 '22 at 10:04
  • To take a step back, what purpose are you trying to achieve with this function? There may be alternative mechanisms that are more reliable or more flexible depending on that (whether it's for debugging which assemblies are loaded when, state initialization, license enforcement or reverse engineering). – Jeroen Mostert Nov 28 '22 at 11:07

0 Answers0