Using a C # Preprocessor to Add a Link

I have a great application that I can create through the command line. I want to specify a flag that allows me to compile it into one of two modes: Actual or Simulated.

So, the main problem is how can I use a preprocessor to programmatically add a link?

For instance:

#if SIMULATED include SimulatedFiles; myFactory = new SimulatedFiles.simFactory(); #else myFactory = new realFactory(); #endif 

I do not want any simulated files to be compiled into my "actual" application. Since there is no "include" directive in C #, I am fixated on how to do this.

+1
source share
4 answers

You cannot do this with the C # preprocessor instruction, because the language does not support the concept of references using preprocessor macros.

What you can do is use the msbuild file and change the set of links added based on the msbuild options.

+10
source

nant / msbuild and dependency injection tool with xml configuration?

+4
source

There is no real preprocessor in C #, as you can read the documentation for C # preprocessors .

From the documentation (my emphasis):

While the compiler does not have a separate preprocessor , the directives described in this section are processed as if they were; these directives are used to aid in conditional compilation. Unlike C and C ++ directives, you cannot use these directives to create macros .

+1
source

Are include files included in your own source code or in third-party DLL assemblies?

If they are your own sources, then you can easily use conditional compilation to remove the “simulated” code from your release build, just like you did in your example (just replace “include” with “using”). This usually applies to debug classes.

If you do not "control" the source code for inclusions, then you can still add a link to the project, but if you conditionally compile all the code that uses the assembly, your application will never try to access the assembly, so it does not need to be present at startup code.

(Another possibility that seems less useful to you is to write the version of the “dummy” reference assembly that you send instead of the “real” one, or a proxy server that calls the real third-party DLL in the simulated assemblies Only if it provides public classes and methods, which you call, you can send the layout, not the simulated assembly to your customers)

+1
source

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


All Articles