why invoking a multi-targeting library doesn`t work as expected?

William Liu 266 Reputation points
2023-02-04T07:27:37.1966667+00:00

I`m currently learning how to create multi-targeting library with dotnet.

I created a multi-targeting library in .net6 and .net 7. However, when I calling the method in this library, the output doesn`t work as my expectation.

excution command:

in powershell, enter the root folder, and excute

dotnet run --project .\src\InvokeTestCon\

expected output:

Hello from NET7_0

Main called in NET7_0

actual output:

Hello from other

Main called in NET7_0

code snippet:

# program.cs

Console.WriteLine(Tools.GetName());

#if NET7_0
Console.WriteLine("Main called in NET7_0");
#endif
# Tools.cs

    public static class Tools
    {
        public static string GetName()
        {
#if NET7_0
            return "Hello from NET7_0";
#else
            return "Hello from other";
#endif
        }
    }

dev environment:

win10 64bit, with .net6.0.405 sdk and .net7.0.102 sdk installed.

I have also uploaded my test code in github. Is there anyone with multi-targeting library experience could help me out?

https://github.com/ali50m/InvokeTest

Thanks!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,387 questions
{count} vote

1 answer

Sort by: Most helpful
  1. William Liu 266 Reputation points
    2023-02-10T03:25:20.3933333+00:00

    After checking the environment on my win10 laptop, I found there was a TargetFramework variable set there. After removing it, it works as expected now.

    I close this question now.

    0 comments No comments