다음을 통해 공유


호환되는 프레임워크 유효성 검사

호환되는 프레임워크를 포함하는 패키지는 한 프레임워크에 대해 컴파일된 코드가 다른 코드에 대해 실행될 수 있는지 확인해야 합니다. 호환되는 프레임워크 쌍의 예는 다음과 같습니다.

  • .NET Standard 2.0 및 .NET 7
  • .NET 6 및 .NET 7

두 경우 모두 소비자는 .NET Standard 2.0 또는 .NET 6에 대해 빌드하고 .NET 7에서 실행할 수 있습니다. 이진 파일이 이러한 프레임워크 간에 호환되지 않으면 소비자가 컴파일 시간 또는 런타임 오류를 직면할 수 있습니다.

패키지 유효성 검사는 패키징 시 이러한 오류를 감지합니다. 예제 시나리오는 다음과 같습니다.

문자열을 조작하는 게임을 작성한다고 가정해 보겠습니다. .NET Framework 및 .NET(.NET Core) 소비자를 모두 지원해야 합니다. 원래 프로젝트는 .NET Standard 2.0을 대상으로 했지만 이제는 불필요한 문자열 할당을 방지하기 위해 .NET 6에서 활용 Span<T> 하려고 합니다. 이렇게 하려면 .NET Standard 2.0 및 .NET 6에 대해 다중 대상을 지정하려고 합니다.

다음 코드를 작성했습니다.

#if NET6_0_OR_GREATER
    public void DoStringManipulation(ReadOnlySpan<char> input)
    {
        // use spans to do string operations.
    }
#else
    public void DoStringManipulation(string input)
    {
        // Do some string operations.
    }
#endif

그 다음 dotnet pack 또는 Visual Studio를 사용하여 프로젝트를 패키징하려고 하면 다음 오류와 함께 실패합니다.

D:\demo>dotnet pack
Microsoft (R) Build Engine version 17.0.0-preview-21460-01+8f208e609 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  PackageValidationThrough -> D:\demo\bin\Debug\netstandard2.0\PackageValidationThrough.dll
  PackageValidationThrough -> D:\demo\bin\Debug\net6.0\PackageValidationThrough.dll
  Successfully created package 'D:\demo\bin\Debug\PackageValidationThrough.1.0.0.nupkg'.
C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Compatibility.Common.targets(32,5): error CP0002: Member 'A.B.DoStringManipulation(string)' exists on lib/netstandard2.0/PackageValidationThrough.dll but not on lib/net6.0/PackageValidationThrough.dll [D:\demo\PackageValidationThrough.csproj]

CompatibleFrameworks

.NET 6을 제외하는 대신 .NET 6에 대한 추가 DoStringManipulation(string) 메서드를 제공해야 함을 깨닫습니다.

#if NET6_0_OR_GREATER
    public void DoStringManipulation(ReadOnlySpan<char> input)
    {
        // use spans to do string operations.
    }
#endif
    public void DoStringManipulation(string input)
    {
        // Do some string operations.
    }

프로젝트를 다시 압축하려고 하면 성공합니다.

호환 가능한 프레임워크 성공적

strict 모드

프로젝트 파일에서 속성을 설정하여 이 유효성 검사기에 대해 strict 모드EnableStrictModeForCompatibleFrameworksInPackage 사용하도록 설정할 수 있습니다. strict 모드를 활성화하면 몇 가지 규칙이 변경되고, 차이점을 찾을 때 다른 규칙이 실행됩니다. 이는 비교하는 양측이 표면적 및 ID에서 엄격하게 동일하도록 하려는 경우에 유용합니다. 자세한 내용은 Strict 모드를 참조하세요.