Monday, May 20, 2013

Linked Files with #If Directives in ASP.NET

Imagine this C#:

#if FOO
   
whatever
#else
   
something more
#endif

 
 

Now imagine that you have a Visual Studio solution with multiple projects and that MyProject.Core contains the code above in a .cs file and moreover has FOO listed within the "Conditional compilation symbols" field within the "Build" tab which one may find by right-clicking on the MyProject.Core project in the Solution Explorer and picking "Properties." Alright, when browsing the .cs file directly at MyProject.Core, the code between #else and #endif above should be greyed out and inaccessible in terms of setting breakpoints.

Linked files as a concept allows one to reference a .cs file in a different project as though it were in the referencing project. Such a file will compile with the assembly that is latching onto it, ending up in the .dll for the referencing assembly with no dependency on the assembly referenced. In this situation, assuming that MyProject.SomeOtherProject has a linked file to the .cs file containing the code at the top of this blog posting and that MyProject.SomeOtherProject does not have FOO at "Conditional compilation symbols" then the first half of the code nested in the #IF Directive, that which comes before #endif, will be unavailable.

No comments:

Post a Comment