Preprocessor directives in different files in C #

I know that I can use preprocessor directives in C# to enable / disable compilation of some part of the code.

If I define a directive in the same file, it works fine:

 #define LINQ_ENABLED using System; using System.Collections.Generic; #if LINQ_ENABLED using System.Linq; #endif 

Now I use C++ to put all these configuration directives inside one header file and include it in all files where I need such directives.

If I do the same in C# , something doesn't work:

 //Config.cs #define LINQ_ENABLED //MyClass.cs #define LINQ_ENABLED using System; using System.Collections.Generic; #if LINQ_ENABLED using System.Linq; #endif 

I also tried the following, but it seems that I cannot define the directive inside the namespace:

 //Config.cs namespace Conf{ #define LINQ_ENABLED } //MyClass.cs #define LINQ_ENABLED using System; using System.Collections.Generic; using Conf; #if LINQ_ENABLED using System.Linq; #endif 
  • What am I doing wrong?
  • What is the correct way to use a preprocessor for different files in C# ?
  • Is there a better way to do this?
+8
c # c-preprocessor conditional-compilation
source share
4 answers

Your .csproj has a section:

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DefineConstants>TRACE;DEBUG;LINQ</DefineConstants> </PropertyGroup> </Project> 

If you need additional preprocessors, you can add them.

Or through project properties that will automatically add them for you. In the properties on the "Assembly" tab.

+9
source share

You can do it for the whole project from Project | Properties

Afaik there is no way to use include files in C #, so there is no easy solution for file groups.

+3
source share

Instead of adding conditional compilation to your files and adding blocks of code that use Linq and others that aren't. I moved all the data access logic (that is, code that has two implementations - with Linq and without) to separate the libraries.

eg. Create the interfaces that will be used by your main application. And create two implementations of these interfaces: one that uses Linq, and the other that don't:

In the main project:

 public interface IUserRepository { IEnumerable<User> GetUsersByCompanyName(string companyName); } 

In Persistence.Linq.dll:

 using System.Linq; public class UserRepository : IUserRepository { public IEnumerable<User> GetUsersByCompanyName(string companyName) // use Linq here } 

In Persistence.SomethingOther.dll:

 public class UserRepository : IUserRepository { public IEnumerable<User> GetUsersByCompanyName(string companyName) // do not use Linq here } 

Now you can embed any implementation of IUserRepository in your main application classes.

+3
source share

It makes no sense to apply #define to usings , since you are not disconnecting libraries from your project, which, I suppose, you would like to avoid the link in some state.

In .NET there is no such thing as a conditional assembly reference (if it is not done manually, dynamically).

Thus, the main point of using preprocessor directives is just to enable/disable portions of the code inside namespaces.

+2
source share

All Articles