Conditional resource compile - c#

I have c# application. The application includes resource file with images and icon.
My target is to compile the same application but with different set of images/icons. Same images name, but different content.
Is there a way to include different resource file in compile time on condition?

Maybe you are looking for Preprocessor Directives or Conditional Attribute.
Preprocessor directives
From this tutorial by Bipin Joshi:
C# preprocessor directives are commands that are meant for the C# compiler. Using preprocessor directives you instruct the C# compiler to alter the compilation process in some way. For example you may instruct the C# compiler that a particular block of code be excluded from the compilation process.
ConditionalAttribute
From MSDN
Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.
To compare these two see this post.

Related

Where to specify preprocessor directives in visual studio?

Trying to define a preprocessor directives in the Visual studio 2012.
#define FLAG
....
#endif
But not sure, where to include this FLAG in visual studio - C#. I remember specifying something like this in C++ projects.
Any thoughts ?
You have two options as to where to define it:
Code file level - In the beginning of the file write #define FLAG. You cannot place anything else (other than comments and blank lines) before define directives. As Ron Beyer points out, a directive defined in a file exists only for that file.
Project level - Right click in the project in Solution Explorer, select Properties, then the Build tab, then look at Conditional compilation symbols. Then one can define several comma-separated symbols there such as: FLAG,FOO,BAR. Note that this symbols list is project configuration dependent (there is a configuration selector in the same tab).
Note as mentioned in the comments, define does not work in C# the same way that it does in C. In C# you just declare that the symbol exists, but you can't assign a value to it. Hence the only use for these symbols is for #if FLAG directives and for the Conditional attribute.
For some reason, when properties clicked, nothing happened for me. So I did the following. And it worked.
Open your csproj project file using text editor and add your preprocessor compiler directives in between these.
<DefineConstants>
HERE!!!
</DefineConstants>

Where will Debug.WriteLine in C# output to when build release?

I put a lot of Debug.WriteLine in my code for debug purposes.
When I am ready to build for release, will these Debug.Write influence the release build, and where do they output to?
Debug.WriteLine is annotated with the Conditional attribute. (see MSDN)
The ConditionalAttribute tells the compiler not to generate that code unless the DEBUG flag is supplied.
From MSDN:
"The ConditionalAttribute attribute is applied to the methods of Debug. Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol."
So if your release configuration does not include DEBUG symbol the Debug.WriteLine calls will be omitted during compilation and there will be no output.
If you switch them to Trace.WriteLine you can define arbitrary listeners in the app/web.config file.
Debug.Writeline is not compiled into release code.
I believe it will be compiled out.

Can I set a C# preprocessor directive from a central file?

I need to add some logging to my app, and it needs to be in release mode, and I need to make sure that the logging code isn't running in production. Therefore I'd like to use a #define and #if to make sure the log statements are removed when I move to production.
Trouble is, I need the logging in multiple files, and it's a pain to put a #define at the top of every file. Is there a way to have a centralized #define? That way I can remove the single #define rather than a #define from all files(which means I'll almost assuredly forget one).
On the command line, use the /define switch. In Visual Studio, choose the "Build" tab from the properties page for the project and fill in the "Conditional Compilation Symbols" section.
Consider also instead of using conditional compilation, to instead make your logging methods conditional methods. That's a more pleasant-looking alternative. That's how Debug.Assert works; it is a conditional method, so if the debug preprocessor symbol is not defined, the compiler simply removes all calls to the method before code generation.
See also my article on the subject:
http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/
Are you using Visual Studio? In the project Properties page, on the "Build" tab, there's a "Conditional compilation symbols" text box.
Yes, this is typically done in your build file, or the script you use which creates your build. You specify it as command-line arguments to MSBuild.
To add to Dave's answer, global conditional compilation symbols can also be specified in Visual.
Right-click on your project and go to Properties
Go to the Build tab
You can specify the symbols that you like (DEBUG is already turned on by default for Debug configurations, so this might actually give you what you want already) for the given configuration, or select "All Configurations" at the top to specify certain symbols for all configurations.
Call the logging everywhere you want.
Define the logging api entry methods with
[Conditional ("DEBUG")]
public void WriteDebugMessage(...)
Build your program in debug mode (which, by default, defines 'DEBUG' in VS). These calls will be part of your assembly.
Build your program in release mode (or - remove the DEBUG symbol from the build definition). These calls are now meaningless no-ops and won't run.
Seems like what you want?

Check using preprocessor if app is a winforms or asp.net

Is this possible to check in assembly what client (winforms app or asp.net page) is running it?
I want to add some methods but only for specific client.
I know, there is predefined DEBUG (#if DEBUG). Where can I find full list, what can I check using preprocessor?
To expand on m0sa's answer, preprocessor directives are basically just a string passed to the compiler.
If you are so inclined, you can add new build configurations (example: instead of Debug/AnyCPU and Release/AnyCPU, you could make WebDebug/AnyCPU, WinformsDebug/AnyCPU, WebRelease/AnyCPU, etc).
Then in the properties page of your project, for each configuration you could provide a value in the 'Conditional compilation symbols' field. For example, for WebDebug and WebRelease, you could provide the conditional symbol WEB. Then, you would be able to use:
#if WEB
using MyNamespace.WebStuff;
#endif
You will need multiple build configurations for that and define different a preprocessor directive for each one. You can set the conditional preprocessor directives in the Build tab of the project Properties page.There are no other directives defined, just the DEBUG directive which you can turn on and off (together with the TRACE directive) in the same tab. Note that DEBUG it is not defined for the "release" build configuration. This is kind of what you will need to do to enable different versions of the assembly to be built.
References:
MSDN
www.davidarno.org <-- see this one for a more visual description

How does C# compile the code with #if preprocessor directive?

I wonder how to compiler compile my code if I using #if directive inside my code.
I would like to create special version of my application (commercial demo version) and I want to limit the functionality of my application. I would rather avoid the obfuscation and just don't want to add all my compiled code to executable file. I need solution resists preview my code during disassembly process. Can I use #if directives for variant compilation instead making comments for disabling code parts?
Using the #if directive is like using the preprocessor in c++, in that the code would simply not be present if the condition hasn't been met (in compilation time). From MSDN:
When the C# compiler encounters an #if
directive, followed eventually by an
#endif directive, it will compile the code between the directives only if
the specified symbol is defined.
Unlike C and C++, you cannot assign a
numeric value to a symbol; the #if
statement in C# is Boolean and only
tests whether the symbol has been
defined or not.
As an addition to #ohadsc's answer: you can always check with Reflector what is actually produced by the compiler.

Categories