6

I have a class library A that is used in other projects in my solution like B and C.

Class library A behaves differently based on the presence of a pre-processor directive, for example :

#if some_directive
      // some code
#else
      // some other code
#end

How can I use class library A in project B with enabled some_directive but use in project C with disabled some_directive?

Naser Asadi
  • 1,153
  • 18
  • 35

3 Answers3

1

You can do something like this using the ConditionalAttribute

This is how Debug.WriteLine() is present or non-present depending on presence of the "DEBUG" symbol.

You will be able to define your own name for the conditional symbol that you use to control the presence or absence of the code.

You can then put that symbol into the list in the "Conditional compilation symbols" settings on the project properties "Build" tab for the project where you want the code.

This doesn't allow you to have the "some other code" part unfortunately, and also it only applies to entire methods.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Yes, but `ConditionalAttribute` works for the project that defines the symbol (project A). I want to have control in project B or C. – Naser Asadi May 14 '13 at 09:42
  • @NaserAsadi The setting as set in project B or C *will* affect the presence or not of the call to the conditional code defined in Project A. This is how you can turn the calls to `Debug.WriteLine()` on and off using the "DEBUG" symbol in your own projects, even though `Debug.WriteLine()` is not defined in your own projects. What it actually does is just make it as if you never called the method if the symbol isn't defined. I guess this might not be very close to what you want though. – Matthew Watson May 14 '13 at 09:50
  • This is actually a really good answer and solves the OP's problem, I think. One drawback though is that the "[Conditional]" members still compile into the final assembly. I personally am looking for a way to do true conditional compilation of an assembly based on which other assembly is referencing it. – Emperor Eto Nov 17 '20 at 15:00
1

It seems that currently this feature is not supported. According to this post:

The language doesn't support the notion of references via preprocessor macros. What you can do is use a msbuild file and alter the set of references added based on msbuild parameters.

Another workaround which I used was using solution configuration in "Configuration Manager". I created two configurations for building each projects B or C which preprocessor directive is enabled in only one of these configurations.

Community
  • 1
  • 1
Naser Asadi
  • 1,153
  • 18
  • 35
0

I Know this topic is old - for me the following approach works nicely and might fit as well: comments of (dis)advantages of this approach are welcome. If you add your .cs-file as existing Item as a link to each project - you can compile it with different directives For adding an existing item as a linked file see screenshots at this post Use folders to organize linked files.

// file ClassA.cs

namespace HelperClasses
{
     public ClassA
    {
    #if some_directive
      // some code
    #else
      // some other code
    #end
    // ....
    }
}

// using statement in Project B and C
using HelperClasses
// Add ClassA.cs in both Projects B and C
// as exiting, linked File -- not as a Reference
// set the compiler-Directives according your needs
Community
  • 1
  • 1
tom_p_zh
  • 11
  • 2