Programmatically compile code using the C# compiler switch

Markus Freitag 3,791 Reputation points
2023-09-28T16:02:38.44+00:00

Hello, How can I compile sequences from certain version

Picture

enter image description here

#if VERSION_7_1 // or higher
            output.ProgramId = "SpecialFlow";
            output.ProgramQty = 454;
#endif

I would like to develop the software further, controls will be added, perhaps also gone.

What is the best way to solve this?
Are there any possibilities? If version is greater than?

VERSION_7_1 is a string

#if VERSION_7_1 || VERSION_7_2 || VERSION_7_3
            output.ProgramId = "SpecialFlow";
            output.ProgramQty = 454;
#endif

#if VERSION_7_4
            ctrl.ProgramId.Visible = false            
#endif

I found this https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

.NET Framework NETFRAMEWORK, NET472, NET472_OR_GREATER How and where can I define this myself?

Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-09-29T01:59:06.1633333+00:00

    Hi @Markus Freitag , Welcome to Microsoft Q&A,

    Symbols in a framework are difficult to change.

    Judging from the existing rules,

    You can use if and elif and else to encapsulate the goals you need.

    #if NET7_0_OR_GREATER
            output.ProgramId = "xxx";
                 output.ProgramQty = xxx;
    #elifxxx
    xxx
    #else
            output.ProgramId = "xxx";
            output.ProgramQty = xxxx;
    #endif
    
    

    If this method doesn't satisfy you, you may be able to manually obtain the version number and make your own judgment.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-09-30T19:22:59.43+00:00

    Conditional compile symbols do not have a value, they are either defined or not. You implement a version greater than symbol by defining it in all versions greater than the desired on. Once you start on this path, you are committed for all later versions.

    #if V10
    #define V_GT_8
    #define V_GT_9
    #elif V9
    #define V_GT_8
    #endif
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.