【问题标题】:.NETStandard multi-targeting library packages gives me CS0234 error on Netstandard 2.0 project.NETStandard 多目标库包在 Netstandard 2.0 项目中出现 CS0234 错误
【发布时间】:2020-11-13 00:30:28
【问题描述】:

所以我必须将一个项目从.NET Framework 重构为Net Standard,Net Standard 版本是2.0,而我的.NET Framework 版本是4.7.2。 我在我的 csproj 中使用多目标,因为我仍然需要来自 .NET Framework 的一些功能,这些功能在 .NET Standard 中不存在,例如,System.Runtime.Remoting。 我认为多目标可以解决我Net Standard 没有包的问题,​​但它给了我这个错误:

错误 CS0234 类型或命名空间名称“Remoting”不存在于 命名空间“System.Runtime”(您是否缺少程序集 参考?)Project.Standard(netstandard2.0)

这是我的 Csproj:

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

    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
        <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    </PropertyGroup>

    <ItemGroup Condition="'$(TargetFramework)'=='net472'">
        <Reference Include="System.Runtime.Remoting" />
    </ItemGroup>


</Project>

由于某种原因,我的代码无法在需要时以 .NET Framework 为目标,还是我的 Csproj 中缺少某些内容?

【问题讨论】:

  • 您正在尝试使用标准中根本不存在的功能(远程处理)。如果其中一个目标只是去return null 或抛出NotSupportedException 或类似的,那么多目标库是没有意义的。

标签: c# asp.net-core .net-standard


【解决方案1】:

您的 CSPROJ 文件已正确设置。我将假设有一些源代码引用了程序集。

您需要确定在源代码中引用它的位置,并将其包含在如下所示的指令中:

public class Class1
{
    public void TestMe()
    {
#if NET472
        var test = new System.Runtime.Remoting.Metadata.SoapAttribute();
#endif
    }
}

这将确保它仅在为 .NET Framework 4.7.2 而不是 .NET Standard 编译时可见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2017-09-15
    • 2019-11-18
    • 2018-03-29
    相关资源
    最近更新 更多