我正在使用在 dotnet core 2.1 中使用EpPlus和freeimage-dotnet-core nuget包的类库。但是,当我想在代码中使用Image
对象时,将显示以下错误:
错误CS0433类型“图像”在两个目录中都存在 'System.Drawing-dotnet-core,版本= 1.2.3.0,文化=中性, PublicKeyToken = null”和“ System.Drawing.Common,版本= 4.0.0.0, 文化=中性,PublicKeyToken = cc7b13ffcd2ddd51'
EpPlus使用System.Drawing.Common dll,而freeimage-dotnet-core使用System.Drawing-dotnet-core,因为在我的类库中,“ system.drawing”命名空间由2个dll确定。
我知道可以使用'extern alias xyz
',但是它可以在dotnet框架完整版上工作,而不是在dotnet核心上工作。
在Dotnet核心项目中,在Visual Studio中,我们看不到dll引用的属性。
我通过在.csproj文件中使用以下代码为我引用的库之一添加别名解决了这个问题:
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'System.Drawing-dotnet-core'">
<Aliases>DrawingCore</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
<ItemGroup>
<PackageReference Include="System.Drawing-dotnet-core" Version="1.2.3" Alias="DrawingCore" />
</ItemGroup>
致谢:@Damien_The_Unbeliever