簡體   English   中英

解決多個標准框架的 NuGet 包 - 可空值問題

[英]NuGet package addressing multiple Standard frameworks - problem with nullables

我們有一系列 .Net Core 3.1 應用程序。 我們將通用代碼抽象為 .NetStandard 2.1 NuGet 包,並在我們的私有 NuGet 服務器上提供這些代碼。

這就是這些 NuGet 包的成功之處,我們被要求將它們提供給我們使用 .Net Framework 4.8 編寫的其他應用程序。

我們遵循了 Microsoft 給出的建議( 在您的項目文件中支持多個 .NET Framework 版本),這對於除我們的一個包之外的所有包都很好。...

那么 - 這個有什么不同?

此包使用可空值。 我可以讓項目編譯得很好,但拋出異常的是測試項目 (.NetCore 3.1)。 我將首先查看我們在 Standard 項目中所做的代碼更改,然后查看 Test 項目 - 希望有人會發現我們出錯的地方。

.Net 標准項目

.csproj 文件

改變了

<TargetFramework>netstandard2.1</TargetFramework>

<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>

還移動了<Nullable>enable</Nullable>到它自己的部分

  <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2_1' ">
    <Nullable>enable</Nullable>
  </PropertyGroup>

班上

然后我們有一個類文件。 我不會費心展示基類,但它遵循類似的模式:

public class MyClass<T> : MyBaseClass
        where T : struct
{
    public MyClass()
        : this(default)
    {
    }

#if netstandard2_1
    public MyClass(T? item)
        : base(true, 0, null)
    {
        this.Item = item;
    }
#else
    public MyClass(T item)
        : base(true, 0, null)
    {
        this.Item = item;
    }
#endif

#if netstandard2_1
    public T? Item { get; set; }
#else
    public T Item { get; set; }
#endif
}

這將編譯並在 bin/release 文件夾(或 bin/debug)中看到兩個文件夾,一個用於netstandard2.1 ,另一個用於netstandard2.0 ,每個文件夾中都有預期的 DLL。

但是,使用 ILSPY 檢查這兩個 DLL 中的此類表明它們是相同的......不是一個可以看到的問號。 不確定這是 ILSPY 的人工制品還是真實的....

測試項目

因此,測試項目具有對標准項目的項目引用(因此,它不是對 NuGet 包的引用)。

在其中一項測試中,我有:

Guid? g = null;
var actual = new ItemValueOperationResponse<Guid>(g);

這導致以下錯誤:

CS1503 參數 1:無法從“System.Guid”轉換? 到“System.Guid”

但是,這個測試項目是.Net Core 3.1,所以它應該使用Standard2.1代碼。

我嘗試交換 *.csproj 中的序列,使其讀取為<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>但無濟於事。

還嘗試使用大寫: #if NETSTANDARD2_1

最終在我們的測試項目中有區域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM