次の方法で共有


異なるランタイムに対してパッケージを検証する

NuGet パッケージ内のランタイムごとに異なる実装アセンブリを使用することを選択できます。 その場合は、これらのアセンブリが互いに互換性があり、コンパイル時アセンブリと互換性があることを確認する必要があります。

たとえば、次のようなシナリオがあるとします。 Unix API と Windows API の相互運用呼び出しを含むライブラリにそれぞれ取り組んでいます。 次のコードを記述しました。

#if Unix
    public static void Open(string path, bool securityDescriptor)
    {
        // Call Unix specific stuff.
    }
#else
    public static void Open(string path)
    {
        // Call Windows specific stuff.
    }
#endif

結果のパッケージ構造は次のようになります。

lib/net6.0/A.dll
runtimes/unix/lib/net6.0/A.dll

lib\net6.0\A.dll は、基になるオペレーティング システムに関係なく、コンパイル時に常に使用されます。 lib\net6.0\A.dll は、Unix 以外のシステムの実行時にも使用されます。 ただし、 runtimes\unix\lib\net6.0\A.dll は Unix システムの実行時に使用されます。

このプロジェクトをパックしようとすると、次のエラーが発生します。

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
  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.Open(string)' exists on lib/net6.0/PackageValidationThrough.dll but not on runtimes/unix/lib/net6.0/PackageValidationThrough.dll [D:\demo\PackageValidationThrough.csproj]
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.Open(string, bool)' exists on runtimes/unix/lib/net6.0/PackageValidationThrough.dll but not on lib/net6.0/PackageValidationThrough.dll [D:\demo\PackageValidationThrough.csproj]

MultipleRuntimes

間違いを認識し、Unix ランタイムにも A.B.Open(string) を追加します。

#if Unix
    public static void Open(string path, bool securityDescriptor)
    {
        // Call Unix specific stuff.
    }

    public static void Open(string path)
    {
        throw new PlatformNotSupportedException();
    }
#else
    public static void Open(string path)
    {
        // Call Windows specific stuff.
    }

    public static void Open(string path, bool securityDescriptor)
    {
        throw new PlatformNotSupportedException();
    }
#endif

プロジェクトをもう一度パックしようとすると、成功します。

MultipleRuntimesSuccessful

厳格モード

プロジェクト ファイルで EnableStrictModeForCompatibleTfmsを設定することで、このバリデーターの厳格モードを有効にすることができます。 厳密モードを有効にすると、一部のルールが変更され、その違いを取得するときに他のいくつかのルールが実行されます。 これは、比較する両側が、そのサーフェス領域と ID で厳密に同じである必要がある場合に便利です。 詳細については、「 厳密モード」を参照してください。