Is there any way with a preprocessor to execute code according to the version of C#?
Example:
#if CSharpVersion = 7.3
var value = 1;
#endif
One option would be to define the LangVersion explicitly in the Project file and have the constants defined based on it. For example,
<LangVersion>7.3</LangVersion>
and
<DefineConstants Condition="'$(LangVersion)' == '7.3'">DEBUG;TRACE;LANG_VERSION_7_3</DefineConstants>
<DefineConstants Condition="'$(LangVersion)' != '7.3'">DEBUG;TRACE;LANG_VERSION_NOT_7_3</DefineConstants>
Now you could use directives as
#if LANG_VERSION_7_3
Console.WriteLine("C# 7_3");
#elif LANG_VERSION_NOT_7_3
Console.WriteLine("Not C# 7_3");
#endif
Please note the LANG_VERSION would signify the compiler accepts syntax specified version or lower.
Related
I have a C# project defining a symbol named WIN:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>$(DefineConstants);WIN</DefineConstants>
</PropertyGroup>
This constant is used in an #if preprocessor directive to conditionally perform some actions, namely, to import some libraries:
#if WIN
const string MyExternalLibrary = #"./lib/Win/MyExternalLibrary.dll";
#else
const string MyExternalLibrary = #"./lib/Linux/MyExternalLibrary.so";
#endif
[...]
[DllImport(MyExternalLibrary )]
public static extern IntPtr MyFunction();
I'd like the WIN constant to be defined, by default, so that the project can be built on windows machines.
At publish time, however, I'd like it to be undefined, to fall in the #else block.
Is there a way to tell dotnet build to undefine a symbol (that is, the opposite of dotnet build's -p switch)?
In C# you can define compile-time constants that may be checked to configure compilation
#define MY_CONST
#if MY_CONST
...
#else
...
#endif
But I can't find a way to see which constants are defined at current line.
I need something like
#warning DEFINED_CONSTANTS that will give me DEBUG; NET5_0
Disregarding everything else, you could just set a field
#if MY_CONST
public static bool IsMyConst = true;
#else
public static bool IsMyConst = false;
#endif
Add pepper and salt to taste.
After some time I've found that it is possible to show defined constants through MSBuild with
<Target Name="ShowConstants" AfterTargets="AfterBuild">
<Warning Text="$(DefineConstants)" />
</Target>
which gives TRACE;DEBUG;NET;NET5_0;NETCOREAPP for net5.0 build.
It doesn't show constants for given line, but at least show constants for current configuration.
I need to get a value from configuration manager in C# Preprocessor directive
And want to do as like below,
#if System.Configuration.ConfigurationManager.AppSettings["Language"].Equals("en-US");
{
bool languageCheck=TRUE
}
#endif
Is it possible ?
No, pre-processors mean "pre-compilation", and at that point it does not know what values are stored in objects or configurations. However, you can add a different build configuration (by clicking on project properties going to build tab), and add the language flag to it to do a similar thing.
public void SayLanguage()
{
#if en_US
Console.WriteLine("en_US");
#else
Console.WriteLine("Language not defined.");
#endif
}
I don't really understand those preprocessor directives what I have to write.
I'm developing a library that should work for many frameworks e.g. .net framework 4.5, 4.6,... and for my application that runs with framework .NETStandard Version 1.5 -> so I guess this is dnxcore50?
public class MyClass
{
#if DOTNET5_4
// do nothing
#else
public void MyMethod()
{
Console.WriteLine("framework is supported");
// and do anything with libraries available in the framework :)
}
#endif
}
so this is what I got for now, but MyMethod is not available with any other framework. Using #if DOTNETCORE50 doesn't work, too.
I also tried to define constraints but my project fails to load when I try this.
Any idea what's the right solution?
There's no need to define them in project/solution-file anymore.
So, I just post an answer matching the current state (a few are still missing like net47 and so on, but you know what I mean):
#if (NET45 || NET451 || NET46 || NET461)
#define NetFramework
#endif
#if (NETSTANDARD1_0 || NETSTANDARD2_0 || NETSTANDARD1_5 || NETSTANDARD1_3 || NETSTANDARD1_6 || NETCOREAPP1_0 || NETCOREAPP1_1 || NETCOREAPP2_0)
#define NetCore
#endif
in what way they are now compatible or redundant, I dont' know. But this is the current names I know.
then you can
public class MyClass
{
#if NetFramework
public void MyMethod()
{
Console.WriteLine("framework is supported");
// and do anything with libraries available in the framework :)
}
#endif
}
In my .net application, I have added new build modes for the configurations.
I currently have: Debug, Release, BetaDebug & BetaRelease.
Each having a specific purpose for software engineering purposes.
I wish to know if I can get a C# string containing this text. So I can then do:
string configurationBuild = GetConfigurationBuild();
if(configurationBuild.Contains("Beta") {
//do something
}
else {
//do something else
}
You can configure that each configuration will define Conditional Compilation symbols use preprocessor instructions like #if to find out which build configurationis being used.
Here is a link on msdn forum http://social.msdn.microsoft.com/Forums/vstudio/en-US/7d574468-c890-49d2-984e-16ad068a006e/build-configuration-in-preprocessor
You can use conditional compilation symbols ( http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(CS.PROJECTPROPERTIESBUILD);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22)&rd=true ) for your build modes.
In your code you can then use the #if directive http://msdn.microsoft.com/de-de/library/4y6tbswk(v=vs.100).aspx