I know that the C# preprocessor will generally eliminate the precompiled symbols. Can the rule be broken a bit?
For example, I have a C# dll A, which I wanna use in another C# program B. In B I have different precompiled symbols X, Y, Z. If X is defined, I need a version of A. Else if Y is defined, I need another version of A. And similar for Z. To implement this, I want to write precompiled directives in A's source code, like
public static class MyClass
public static void MyMethod()
In general, you define compiler constants on a project basis. So if your shared assembly defines
X and you compile it, it only contains the call to
DoX(). Then you compile the actually consuming application where you define
Y, but that won't affect the shared assembly.
However, compiler constants can be passed to the compiler using the command line or using XAML, and then apply to all projects that are built at the same time.
How to do so is explained in Is There anyway to #define Constant on a Solution Basis? and MSDN: How to: Declare Conditional Compilation Constants.
So that might be an option: declare them solution-wide and compile the solution as a whole. Do note that your shared assembly then only contains the call corresponding to the constant(s) defined at compile-time: you can't share that assembly with different executables compiled with different constants, because only
DoX() will be called if
X was declared at compile-time.
Or, rather, you dump this idea altogether and implement the proper design pattern, so the proper method will be called depending on what the caller wants, not who the caller is.
This may mean you should simply expose
DoZ() to the caller, or let
MyMethod() do something based on a passed (enum) variable, or something using a strategy pattern.
That being said, there may be reasons to do what you're trying to do, for example not leaking implementation details to competing companies who use your application.