0

I am working on a class library that uses EpPlus and freeimage-dotnet-core nuget packages in dotnet core 2.1. But when i want to use Image object in my code the below error is shown:

Error CS0433 The type 'Image' exists in both 'System.Drawing-dotnet-core, Version=1.2.3.0, Culture=neutral, PublicKeyToken=null' and 'System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

EpPlus uses System.Drawing.Common dll and freeimage-dotnet-core uses System.Drawing-dotnet-core, because of that in my class library "system.drawing" namespace determined in 2 dlls.

I know it is possible to use 'extern alias xyz', but it works on dotnet framework full version,not in dotnet core. in Dotnet core projects, in visual studio we cannot see properties of a dll reference.


I solved the problem with adding an alias for one of my referenced libraries with this codes in .csproj file:

<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>

Thnanks to: @Damien_The_Unbeliever

isaeid
  • 543
  • 5
  • 26
  • System.Drawing-dotnet-core is not a namespace name, is name of dll file. however in namespace names we cannot use "-" character. @Reniuz – isaeid Jan 15 '19 at 09:20
  • Ah sorry misunderstood :) – Renatas M. Jan 15 '19 at 09:23
  • The error message indicates you have the same object in two different namespaces and/or classes. So the error can be fixed by references the namespace along with the class name. Suppose you have Class1 in both Namespace 1 and Namespace2. Then you use Namespace1.Class1 or Namespace2.Class1. – jdweng Jan 15 '19 at 09:51
  • You could also alias the `Image` class and use the alias in your code: `using CoreImage = System.Drawing-dotnet-core.Immage;` – Justinas Marozas Jan 15 '19 at 09:55
  • BTW, those are `using` directives, not `using` statements. The latter are the things found inside method bodies being used to ensure clean-up of resources via `Dispose`. – Damien_The_Unbeliever Jan 15 '19 at 10:39
  • @jdweng, the error indicates because 'One' object determined in same namespaces. And the same namespaces determined in different dll's which referenced in my project. – isaeid Jan 15 '19 at 10:47
  • @JustinasMarozas , As i said in previews comment, we cannot use "-" in namespace names, and "System.Drawing-dotnet-core" is name of a dll reference not a namespace name. – isaeid Jan 15 '19 at 10:51
  • @Damien_The_Unbeliever, OK, but "using" is "directive" or "statement"?, it is not my problem. We are programmers, gaming with words is philosopher's job. – isaeid Jan 15 '19 at 10:57
  • No, the C# language *defines* two of it's features that both use the `using` keyword. It *defines* one of them, the using directive, as something that logically brings namespaces into scope. It *defines* the other one of them, the using statement, as something that achieves resource cleanup. Using a pet name for something is sometimes okay but when you use a pet name that has a *specific* other meaning in the programming language you're working in, not so good. – Damien_The_Unbeliever Jan 15 '19 at 10:57
  • @Damien_The_Unbeliever, Yes i know. thank you. I am reading your linked question. – isaeid Jan 15 '19 at 11:07

0 Answers0