Conditional compilation depending on the Framework

zequion 446 Reputation points
2024-05-22T05:56:25.7866667+00:00

VC employment with Winforms, WPF and Asp.Net. All projects use the same dlls in C#.

I am trying to go from .Net framework 4.8 to .Net 8 and I need to include some code in some dlls so that, at compile time, it detects whether 4.8 or .Net is being used and, depending on the type of Framework, compiles certain lines or not . I may even need it to compile one function or another.

Is there any C# code example that shows how to do it?

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

Accepted answer
  1. Anonymous
    2024-05-22T08:33:18.7933333+00:00

    Hi @zequion , Welcome to Microsoft Q&A,

    Make sure that the correct compilation symbol definitions are included in the project file.

    .NET Framework 4.8 project file (.csproj):

    xmlCopy code
    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    

    .NET 8 project file (.csproj):

    xmlCopy code
    <Project Sdk="Microsoft.NET.Sdk">
    

    Create a class library project and use conditional compilation directives in your code. This class library will generate DLLs containing code for different frameworks.

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFrameworks>net48;net8.0</TargetFrameworks>
      </PropertyGroup>
      <PropertyGroup Condition="'$(TargetFramework)' == 'net48'">
        <DefineConstants>NET48</DefineConstants>
      </PropertyGroup>
      <PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
        <DefineConstants>NET8</DefineConstants>
      </PropertyGroup>
    </Project>
    

    Use #if directives in shared class library projects to distinguish code from different frameworks.

    using System;
    
    public class FrameworkSpecificClass
    {
        public void FrameworkSpecificMethod()
        {
            #if NET48
            Console.WriteLine("Running on .NET Framework 4.8");
            #elif NET8
            Console.WriteLine("Running on .NET 8");
            #else
            Console.WriteLine("Running on an unknown framework");
            #endif
        }
    
        public void SpecificMethod()
        {
            #if NET48
            SpecificMethodForNet48();
            #elif NET8
            SpecificMethodForNet8();
            #endif
        }
    
        #if NET48
        private void SpecificMethodForNet48()
        {
            Console.WriteLine("Specific method for .NET Framework 4.8");
        }
        #elif NET8
        private void SpecificMethodForNet8()
        {
            Console.WriteLine("Specific method for .NET 8");
        }
        #endif
    }
    

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.