Run code during assembly

I am working on a wrapper for a huge unmanaged library. Almost every function can call some error handler internally. The default error handler writes an error to the console and calls the abort () function. This behavior is undesirable for a managed library, so I want to replace the default error handler with my own, which will simply throw some kind of exception and allow the program to continue normal execution after handling this exception. The error handler must be modified before any of the wrapped functions are called.
The wrapper library is written in managed C ++ with a static link to the wrapped library, so nothing like the "type with hundreds of dll imports" is present. I also cannot find one type that is used by everyone inside the wrapper library. Therefore, I can not solve this problem by specifying a static constructor in one type that will execute the code that I need.

Currently, I see two ways to solve this problem:

  • Define some static method, for example, Library.Initialize (), which must be called by the client with one click before its code will use any part of the wrapper library.

  • Find the smallest subset of types that each top-level function uses (I think the size of this subset will be about the same as the 25-50 types) and add the static constructors calling Library.Initialize (which will be internal in this scenario) for each of these types.

I read this and this question, but they did not help me. Are there any suitable ways to solve this problem? Maybe there are some nice hacks?

+3
source share
3 answers

A few other suggestions:

  • Create an abstract base class as the root for all wrapper classes and place the initialize call in the constructor for the base class.
  • factory, .
+1

, 2 . , . , Library.initialize, .

0

, , -, , . .

0

Source: https://habr.com/ru/post/1710385/


All Articles