.NET多目标框架构建错误:找不到NeutralResourcesLanguageAttribute
尝试将旧.NET Framework项目转为同时面向net481和net8.0-windows的双目标项目时,持续触发以下构建错误:
无法在程序集'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8.1\Facades\System.Runtime.dll'中找到类型'System.Resources.NeutralResourcesLanguageAttribute'。
已尝试删除bin和obj目录、重启Visual Studio等操作,均未解决问题。确认该属性存在于.NET 4.8.1中,但不清楚报错原因。
项目的.csproj文件内容如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <UseWindowsForms>true</UseWindowsForms> <UseWpf>true</UseWpf> <Deterministic>false</Deterministic> <PlatformTarget>x64</PlatformTarget> <Platforms>x64</Platforms> <AssemblyName>JacobsRevitTools</AssemblyName> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <TargetFrameworks>net481;net8.0-windows</TargetFrameworks> <StartAction>Program</StartAction> <StartProgram>c:\Program Files\Autodesk\Revit 2025\Revit.exe</StartProgram> <Nullable>disable</Nullable> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <DefineConstants>$(DefineConstants);REVIT2019;REVIT2020;REVIT2021;REVIT2022;REVIT2023;REVIT2024;REVIT2025</DefineConstants> </PropertyGroup> <ItemGroup> <PackageReference Include="EPPlus" Version="7.4.0" /> <PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.1" /> <PackageReference Include="NUnit" Version="4.2.2" /> <PackageReference Include="SmartFormat.NET" Version="3.5.0" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.1" /> <PackageReference Include="System.Drawing.Common" Version="8.0.8" /> <PackageReference Include="System.Resources.ResourceManager" Version="4.3.0" /> <PackageReference Include="System.Runtime.Caching" Version="8.0.1" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\JacobsToolsCommon\JacobsToolsCommon.csproj" /> <ProjectReference Include="..\JacobsToolsUI\JacobsToolsUI.csproj" /> </ItemGroup> <ItemGroup> <Reference Include="AdWindows"> <HintPath>..\..\..\..\..\..\Program Files\Autodesk\Revit 2025\AdWindows.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="RevitAPI"> <HintPath>..\..\..\..\..\..\Program Files\Autodesk\Revit 2025\RevitAPI.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="RevitAPIUI"> <HintPath>..\..\..\..\..\..\Program Files\Autodesk\Revit 2025\RevitAPIUI.dll</HintPath> <Private>True</Private> </Reference> </ItemGroup> <ItemGroup> <Compile Update="Properties\Settings.Designer.cs"> <DesignTimeSharedInput>True</DesignTimeSharedInput> <AutoGen>True</AutoGen> <DependentUpon>Settings.settings</DependentUpon> </Compile> </ItemGroup> <ItemGroup> <None Update="Properties\Settings.settings"> <Generator>SettingsSingleFileGenerator</Generator> <LastGenOutput>Settings.Designer.cs</LastGenOutput> </None> </ItemGroup> </Project>
解决方法
1. 移除不必要的System.Resources.ResourceManager包
在.NET Framework 4.8.1中,System.Resources.ResourceManager属于基础类库,无需通过NuGet引用。该包是为.NET Standard/.NET Core设计的,双目标时会干扰.NET Framework的程序集解析。直接删除对应的PackageReference:
<!-- 删除这一行 --> <PackageReference Include="System.Resources.ResourceManager" Version="4.3.0" />
2. 为.NET Framework目标添加程序集绑定重定向(可选)
如果删除包后仍有问题,可在项目中添加app.config(已存在则修改),添加绑定重定向确保System.Runtime指向正确的.NET Framework程序集:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.2.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
3. 检查项目引用的依赖项是否兼容双目标
确认所有PackageReference和ProjectReference都支持net481和net8.0-windows。例如,部分旧包可能仅支持.NET Framework,需升级到兼容版本;或者通过条件引用为不同目标框架指定合适的包版本:
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.1" Condition="'$(TargetFramework)' == 'net481'" /> <PackageReference Include="Extended.Wpf.Toolkit" Version="5.0.0" Condition="'$(TargetFramework)' == 'net8.0-windows'" />
4. 清理并重新生成解决方案
- 删除所有项目的
bin和obj目录 - 在Visual Studio中执行清理解决方案,再执行重新生成解决方案
- 若仍有问题,尝试使用命令行构建:
dotnet clean dotnet build --configuration Debug
内容的提问来源于stack exchange,提问作者sean2000

