45

Is it possible to run some code when an assembly is loaded, without doing anything specific in the loading code? What I am looking for is like a static constructor on a type.

Eg:

Assembly A does not know about Assembly B, but B does know about A. Assembly A needs to know certain things about B if B is loaded. When Assembly B is loaded by the runtime (referenced, or explicit), I want a piece of code (static method or attribute) to be executed that calls a method in Assembly A.

The root cause of this problem is unknown types being encountered when serializing a type in A that contains types from B not known at compile time as interfaces are used.

starblue
  • 55,348
  • 14
  • 97
  • 151
Robert Wagner
  • 17,515
  • 9
  • 56
  • 72

6 Answers6

47

The CLR supports module initializers. You'd have to hack C++/CLI code or ilasm.exe to use them.

UPDATE: directly supported in C# as well since .NET 5 with the [ModuleInitializer] attribute

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
7

There are 3 options to initialize a .NET Assembly:

  1. You write a static function Init() or Main() in your Assembly to be initialized and call this function by reflection from the C# code that loads this Assembly.
  2. Write a Managed C++ Assembly where you put your code in DllMain(). Be careful because your code will be executed in the Loader Lock where several things are forbidden (like loading other DLL's,...). But you can start a new thread that does ANY initialization stuff. (About LoaderLock: https://msdn.microsoft.com/en-us/library/ms173266.aspx) (About DllMain: C# to C++/CLI to C DLL System.IO.FileNotFoundException)
  3. You compile a pure C# Assembly and modify the compiled DLL to add a module initializer code like explained here: http://einaregilsson.com/module-initializers-in-csharp/ The disadvantage of this method is that the initialization function is not called immediately when the assembly is loaded into the process. But it is called before anything else in the assembly is first accessed.
Community
  • 1
  • 1
Elmue
  • 7,602
  • 3
  • 47
  • 57
7

You can use static constructors in .Net, but unfortunately they don't do what you want. Static constructors are only executed just before a type is used. See http://msdn.microsoft.com/en-us/library/k9x6w0hc(VS.80).aspx for details.

You might get some mileage from subscribing to your AppDomain's AssemblyLoad event. See http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyload.aspx.

In your event handler you could reflect on the newly loaded assembly, and get it to execute whatever code you like.

Antony Perkov
  • 931
  • 6
  • 13
  • Unfortuantly since only one out of the 30 odd assemblies needs this registering done, AssemblyLoad may be a bit over overkill. – Robert Wagner Feb 02 '09 at 23:14
6

(edit - applies to C#; for a C++ approach, see this answer)

Basically, no: you can't. This would be a huge attack surface, and isn't allowed. You might want to put a static ctor on some of the B types that ensure the init code is executed, but that is about it...

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    It seems that it would be fairly easy to hide code in there that no code knowingly invokes - hard to trace, etc. Let alone the question of which thread should run it... Maybe I can't vocalize it properly, but it sounds unwise to me. But it is moot since you can't do it anyway ;-p – Marc Gravell Feb 02 '09 at 22:57
  • Do you know if there is any information on it (module initializers don't run on Load, but rather before static constructors), so it is interesting if code can run during Load itself. If you to check if you can answer this very close question too - http://stackoverflow.com/questions/24718917/can-a-call-to-assembly-loadbyte-raise-the-appdomain-assemblyresolve-event/24719526?noredirect=1#comment38423553_24719526 – Alexei Levenkov Jul 15 '14 at 16:50
3

You should probably revisit your serialization approach to mitigate this problem. If you serialize using ISerializable and the SerializableAttribute attribute, you can make it such that the serialization graph will load assembly B when necessary without assembly A ever having to explicitly know about assembly B.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • How can I identify and load Assembly B using this method? – Robert Wagner Feb 02 '09 at 23:06
  • If you serialize using, say a BinaryFormatter, any types in your serialization graph that implement ISerializable will have their constructors called during the deserialize, in which you can call whatever code you need. As, when you serialize, the serialization queries for the interface... – Jeff Yates Feb 02 '09 at 23:11
  • ... it doesn't matter if A only refers to B's types by an interface, they will serialize properly. – Jeff Yates Feb 02 '09 at 23:12
  • We are using XML Serialization with WCF. – Robert Wagner Feb 02 '09 at 23:16
  • Then you may well be clean out of luck with this approach. There is a SoapFormatter that persists ISerializable types to SOAP XML and there are XmlFormatter's available online (though I don't know how successful they are). – Jeff Yates Feb 02 '09 at 23:18
0

Using a mixed assembly you can get DllMain to run on an assembly load.

Mike
  • 1